#!/usr/bin/env perl # "Simplified Perl Status Poster" # # MIT License # # Copyright (c) 2023 Samir Parikh # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. use warnings; use strict; use lib "/home/compiler/perl5/lib/perl5"; use JSON; use POSIX qw( strftime ); use Data::Dumper; use feature qw( signatures ); no warnings qw( experimental::signatures ); ### declare constants my $POST_DATA_FILENAME = "/home/compiler/programs/spsp/html/post_data"; my $TEMP_FILE = "/home/compiler/programs/spsp/html/tmp.$$"; my $ID_LENGTH = 4; my $ID = "id"; my $TITLE = "title"; my $STATUS = "status"; my $TIMESTAMP = "timestamp"; sub generate_post_id ( $id_length ) { my @characters = (0 .. 9, "a" .. "z"); return join '', map $characters[ rand @characters ], 1 .. $id_length; } ### slurp the input into a scalar ### https://www.perl.com/article/21/2013/4/21/Read-an-entire-file-into-a-string/ chomp( my $status = do { local $/; <> } ); ### check to see if post_data file exists. If not, create it unless ( -e $POST_DATA_FILENAME ) { open my $post_data_filehandle, '>>', $POST_DATA_FILENAME or die "Cannot open $POST_DATA_FILENAME for appending: $!\n"; close $post_data_filehandle or die "Error closing $POST_DATA_FILENAME: $!\n"; } ### open post data file for reading open my $post_data_filehandle, '<', $POST_DATA_FILENAME or die "Cannot open $POST_DATA_FILENAME for reading: $!\n"; ### read all current posts into @posts my $json_posts = JSON->new; my @posts = do { local $/; @{ $json_posts->decode( <$post_data_filehandle> ) }; }; ### close post data file close $post_data_filehandle or die "Cannot close $POST_DATA_FILENAME: $!\n"; ### assemble the new post into a hash my %post = ( $ID => generate_post_id( $ID_LENGTH ), $TITLE => '', $STATUS => $status, $TIMESTAMP => strftime "%a %d %b %Y %H:%M %Z", localtime, ); ### prepend new post to the array of posts unshift @posts => \%post; # write updated posts to temporary file ### open temporary file for writing open my $temp_filehandle, '>', $TEMP_FILE or die "Cannot open $TEMP_FILE for writing: $!\n"; my $posts = $json_posts->pretty->encode( \@posts ); print $temp_filehandle $posts; ### close the temporary file close $temp_filehandle or die "Error closing $TEMP_FILE: $!\n"; ### delete original post data file unlink $POST_DATA_FILENAME or die "Cannot delete original $POST_DATA_FILENAME: $!\n"; ### Rename temporary file to replace original post data file rename $TEMP_FILE, $POST_DATA_FILENAME or die "Cannot rename $TEMP_FILE to $POST_DATA_FILENAME: $!\n"; ##################################################################### print "see if we can now read the file contents\n"; open $post_data_filehandle, '<', $POST_DATA_FILENAME or die "Cannot open $POST_DATA_FILENAME: $!\n"; while ( <$post_data_filehandle> ) { chomp; print "$_\n"; # $data = $json_decode->decode( $_ ); # print $data; } ### close post data file close $post_data_filehandle or die "Cannot close $POST_DATA_FILENAME: $!\n"; ###################################################################### exit 0;