Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
#! /usr/bin/perl -wT # ###################### # # notepad.pl # v0.10 # Michael K. Neylon # mneylon-pm@masemware.com # # Creates a 'notes-to-self' page, which you can write notes to yoursel +f, # and also delete those notes after you no longer need them. Ideal us +e # would be for one with multiple connect points to the internet (work +& # home), and with own private server that you want to drop notes onto. + # Note that there is NO security built into this module; this should b +e # done at the server level. # # Requires nothing beyond a standard perl install (uses CGI.pm). # # Configuration options: # $notes_dir : Location of directory where data should be stored fro +m # the script. This *should* be absolute unless you kno +w # for sure what directory your scripts start up in. # # Suggestions/Ideas/Comments please email me at mneylon-pm@masemware.c +om # ###################### use strict; use CGI; #### CONFIGURATION #### # $notes_dir : Location of directory where data should be stored fro +m # the script. This *should* be absolute unless you kno +w # for sure what directory your scripts start up in. my $notes_dir = '/home/services/www/notes/data'; #### END OF CONFIGURATION #### # Typical starting point my $cgi = new CGI; print $cgi->header(); print $cgi->start_html( -title=>'Personal Notemaker' ); print $cgi->h1( { -align=>'center'}, 'Personal Notemaker' ); # Since this page is pretty much a single form... print $cgi->start_form; ### # Process the changes from the last screen ### # First, if a new note has been made, write it out. my $note = $cgi->param( 'new_note' ) || ''; $note =~ s/^\s*//; $note =~ s/\s*$//; if ( $note ne '' ) { my $time = time(); # note that in case we suddently go to 64 bit time, 20 decimal place +s # should be enough to take care of it. But as long as the filename # is right justified and zero padded, the script won't care. open ( FILE, ">$notes_dir/" . sprintf( "%020d", $time ) . '.txt' ) o +r die "Can't write out new note : $!"; print FILE $note; close FILE; $cgi->delete( 'new_note' ); # So that it doesn't show up again... } # Now handle any notes for deletion - must be checked to be removed foreach my $param ( $cgi->param ) { if ( ( $cgi->param( $param ) eq 'on' ) && ( $param =~ /^delete_(.*)$ +/ ) ) { my $file = $1; if ( -e "$notes_dir/$file.txt" ) { unlink "$notes_dir/$file.txt" or die "Can't delete note file : $ +!"; } } } ### # Present a slot for a new note ### print $cgi->div( { -align=>'center' }, $cgi->p( $cgi->b( "New note" ) ), $cgi->textarea( -name => 'new_note', -rows => 4, -columns => 80) ); print $cgi->hr; ### # Write existing notes to output ### # First open and grab list of files... opendir( DIR, $notes_dir ) or die "Can't open notes directory : $!"; my @files = readdir( DIR ) or die "Can't read notes directory : $!"; closedir( DIR ); # readdir sucks in . and .., so strip it via grep @files = grep { /^.*\.txt$/ } @files; # Since filenames are stored via unix time, it's simply a matter of so +rting # them now, in ascending date order (though these are still strings, s +o cmp) @files = sort { $a cmp $b } @files; # Now we've got a sorted list of files that contain our notes. Presen +t them. # Note that to avoid memory taxing, we'll won't try to get fancy with +the # table output. # But of course, if no output, let's not do this... if ( scalar @files == 0 ) { print $cgi->p( { -align=>'center'}, 'You have no notes waiting.' ); } else { print $cgi->start_table( { -width => '100%' } ); print $cgi->Tr( $cgi->th( ['Delete', 'Message', 'Time' ] ) ); foreach my $file ( @files ) { # time data is set in the file name. my ($time) = ( $file =~ /^(.*)\.txt/ ); my $ltime = localtime( $time ); # Open the file and read in data open( FILE, "<$notes_dir/$file" ) or die "Can't open a note file $file : $!"; my @lines = <FILE>; close( FILE ); my $text = join('', @lines); # Print the table row. print $cgi->Tr( $cgi->td( { -align=>'center' }, $cgi->checkbox( -name => "delete_$time", -label => '' ) ), $cgi->td( { -width=>'80%' }, $text ), $cgi->td( $cgi->small( $ltime ) ) ); } print $cgi->end_table; } # Submit button print $cgi->p( { -align => 'center' }, $cgi->submit ) ; # Clean up print $cgi->end_form; print $cgi->end_html; 1;

In reply to Notepad.pl by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (19)
    As of 2014-08-27 13:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (238 votes), past polls