Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (None)
    As of 2024-04-25 00:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found