spsp

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

commit cdafa972f61780ab296c62e6824ba0ee62d9690d
parent d8b2e30c9b4e6995e14e350df8828e9545bd56d5
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 26 Jan 2023 17:20:39 +0000

update program to use Path::Tiny to allow us to prepend the latest
status update to the top.  Also included logic to check that post_data
file already exists.  If it doesn't we create a new blank file

Diffstat:
Mspsp | 30++++++++++++++----------------
1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/spsp b/spsp @@ -28,6 +28,7 @@ use warnings; use strict; use JSON; use POSIX qw( strftime ); +use Path::Tiny; # to prepend data to post_data file use feature qw( signatures ); no warnings qw( experimental::signatures ); @@ -47,31 +48,28 @@ sub generate_post_id ( $id_length ) chomp( my $status = do { local $/; <> } ); -my %post = ( +### 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"; +} + +my $content = path( $POST_DATA_FILENAME )->slurp_utf8; + +my %post = ( $ID => generate_post_id( $ID_LENGTH ), $TITLE => '', $STATUS => $status, $TIMESTAMP => strftime "%a %d %b %Y %H:%M %Z", localtime, ); -#print "id: $post{ $ID }\n"; -#print "status: $post{ $STATUS }\n"; -#print "time: $post{ $TIMESTAMP }\n"; - my $json = JSON->new; my $json_post = $json->pretty->encode( \%post ); print $json_post; -### Open the data file for concatenation, and die upon failure -open my $post_data_filehandle, '>>', $POST_DATA_FILENAME - or die "Cannot open $POST_DATA_FILENAME for appending: $!\n"; - -### Insert the new post into the file -print $post_data_filehandle $json_post - or die "Error writing post to $POST_DATA_FILENAME: $!\n"; - -### Close the post data file -close $post_data_filehandle or die "Error closing $POST_DATA_FILENAME: $!\n"; +path( $POST_DATA_FILENAME )->spew_utf8( $json_post, $content ); exit; -