spsp

Repository for the "Simplified Perl Status Poster" (SPSP).
git clone git://git.samirparikh.com/spsp
Log | Files | Refs | README

commit ec16824a8645bb6f88e86863bc2ed6517e80eeb5
parent 243ee6026c41786b34043c25517ce8fb3bae9c33
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Wed,  1 Feb 2023 14:03:12 +0000

update spsp to update post_data file with status entries stored
as array of hashes

Diffstat:
Mspsp | 59+++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 45 insertions(+), 14 deletions(-)

diff --git a/spsp b/spsp @@ -29,13 +29,13 @@ use strict; use lib "/home/compiler/perl5/lib/perl5"; use JSON; use POSIX qw( strftime ); -use Path::Tiny; # to prepend data to post_data file 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"; @@ -61,8 +61,21 @@ unless ( -e $POST_DATA_FILENAME ) close $post_data_filehandle or die "Error closing $POST_DATA_FILENAME: $!\n"; } -my $content = path( $POST_DATA_FILENAME )->slurp_utf8; +### 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 => '', @@ -70,25 +83,43 @@ my %post = ( $TIMESTAMP => strftime "%a %d %b %Y %H:%M %Z", localtime, ); -my $json = JSON->new; -my $json_post = $json->encode( \%post ); -print $json_post, "\n"; +### 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; -path( $POST_DATA_FILENAME )->spew_utf8( $json_post, $content ); +### 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 my $post_data_filehandle, '<', $POST_DATA_FILENAME +open $post_data_filehandle, '<', $POST_DATA_FILENAME or die "Cannot open $POST_DATA_FILENAME: $!\n"; -my $data; -my $json_decode = JSON->new; - while ( <$post_data_filehandle> ) { chomp; - #print "$_\n"; - $data = $json_decode->decode( $_ ); - print $data; + print "$_\n"; +# $data = $json_decode->decode( $_ ); +# print $data; } -exit; +### close post data file +close $post_data_filehandle or die "Cannot close $POST_DATA_FILENAME: $!\n"; +###################################################################### + +exit 0;