spsp

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

spsp (4348B) - raw


      1 #!/usr/bin/env perl
      2 
      3 # "Simplified Perl Status Poster"
      4 # 
      5 # MIT License
      6 # 
      7 # Copyright (c) 2023 Samir Parikh
      8 # 
      9 # Permission is hereby granted, free of charge, to any person obtaining a copy
     10 # of this software and associated documentation files (the "Software"), to deal
     11 # in the Software without restriction, including without limitation the rights
     12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     13 # copies of the Software, and to permit persons to whom the Software is
     14 # furnished to do so, subject to the following conditions:
     15 # 
     16 # The above copyright notice and this permission notice shall be included in all
     17 # copies or substantial portions of the Software.
     18 # 
     19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     25 # SOFTWARE.
     26 
     27 use warnings;
     28 use strict;
     29 use lib "/home/compiler/perl5/lib/perl5";
     30 use JSON;
     31 use POSIX   qw( strftime );
     32 use Data::Dumper;
     33 use feature qw( signatures );
     34 no warnings qw( experimental::signatures );
     35 
     36 ### declare constants
     37 my $POST_DATA_FILENAME = "/home/compiler/programs/spsp/html/post_data";
     38 my $TEMP_FILE          = "/home/compiler/programs/spsp/html/tmp.$$";
     39 my $ID_LENGTH          = 4;
     40 my $ID                 = "id";
     41 my $TITLE              = "title";
     42 my $STATUS             = "status";
     43 my $TIMESTAMP          = "timestamp";
     44 
     45 sub generate_post_id ( $id_length )
     46 {
     47     my @characters = (0 .. 9, "a" .. "z");
     48     return join '', map $characters[ rand @characters ], 1 .. $id_length;
     49 }
     50 
     51 ### slurp the input into a scalar
     52 ### https://www.perl.com/article/21/2013/4/21/Read-an-entire-file-into-a-string/
     53 chomp( my $status = do { local $/; <> } );
     54 
     55 ### check to see if post_data file exists.  If not, create it
     56 unless ( -e $POST_DATA_FILENAME )
     57 {
     58     open my $post_data_filehandle, '>>', $POST_DATA_FILENAME
     59         or die "Cannot open $POST_DATA_FILENAME for appending: $!\n";
     60     
     61     close $post_data_filehandle or die "Error closing $POST_DATA_FILENAME: $!\n";
     62 }
     63 
     64 ### open post data file for reading
     65 open my $post_data_filehandle, '<', $POST_DATA_FILENAME
     66     or die "Cannot open $POST_DATA_FILENAME for reading: $!\n";
     67 
     68 ### read all current posts into @posts
     69 my $json_posts = JSON->new;
     70 my @posts      = do {
     71                         local $/;
     72                         @{ $json_posts->decode( <$post_data_filehandle> ) };
     73                     };
     74 
     75 ### close post data file
     76 close $post_data_filehandle or die "Cannot close $POST_DATA_FILENAME: $!\n";
     77 
     78 ### assemble the new post into a hash
     79 my %post      = (
     80                 $ID         => generate_post_id( $ID_LENGTH ),
     81                 $TITLE      => '', 
     82                 $STATUS     => $status,
     83                 $TIMESTAMP  => strftime "%a %d %b %Y %H:%M %Z", localtime,
     84 );
     85 
     86 ### prepend new post to the array of posts
     87 unshift @posts => \%post;
     88 
     89 # write updated posts to temporary file
     90 ### open temporary file for writing
     91 open my $temp_filehandle, '>', $TEMP_FILE
     92     or die "Cannot open $TEMP_FILE for writing: $!\n";
     93 
     94 my $posts = $json_posts->pretty->encode( \@posts );
     95 print $temp_filehandle $posts;
     96 
     97 ### close the temporary file
     98 close $temp_filehandle or die "Error closing $TEMP_FILE: $!\n";
     99 
    100 ### delete original post data file
    101 unlink $POST_DATA_FILENAME
    102     or die "Cannot delete original $POST_DATA_FILENAME: $!\n";
    103 
    104 ### Rename temporary file to replace original post data file
    105 rename $TEMP_FILE, $POST_DATA_FILENAME
    106     or die "Cannot rename $TEMP_FILE to $POST_DATA_FILENAME: $!\n";
    107 
    108 #####################################################################
    109 print "see if we can now read the file contents\n";
    110 open $post_data_filehandle, '<', $POST_DATA_FILENAME
    111     or die "Cannot open $POST_DATA_FILENAME: $!\n";
    112 
    113 while ( <$post_data_filehandle> )
    114 {
    115     chomp;
    116     print "$_\n";
    117 #    $data = $json_decode->decode( $_ );
    118 #    print $data; 
    119 }
    120 
    121 ### close post data file
    122 close $post_data_filehandle or die "Cannot close $POST_DATA_FILENAME: $!\n";
    123 ######################################################################
    124 
    125 exit 0;