Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Run with -w and use strict to ensure Perl isn't assuming things incorrectly. Check all return values. Include the ability to turn on debugging (e.g. print different output depending on the value of my $debug). Use $debug=-1; if yu you want no output except errors (e.g. for cronjobs), $debug=0 for the default when run from the command line, $debug>0 when trying to debug the program. Larger values of $debug result in more informative output.  You can decide whether you are being called from a tty (the command line, NOT a cronjob) by using:

Code Block

my $debug; #For cronjobs use -1, for normal execution from command line use 0,
                  #for debugging information usr > 0
if (-t STDOUT) {$debug=0;}  #scripot executed from tty (command line)
else               {$debug=-1;} #script executed from cronjob

CGI scripts 

These run as user nobody and need to be super secure. Read the Oerl Perl Cookbook and "Writing a Safe Secure CGI program". Always use the taint mode (-T).  

...

No Format
$ perltidy file
# simple example perltidy script
my $input = <STDIN>;
if ( open( FILE, "<$input" ) ) {
    while ( $file = <FILE> ) {

        # pointless loop!
        for ( my $i = 0 ; $i < 100 ; $i++ ) { print "."; }
        print "\n";
        push( @entries, $file );    # copy contents of file to memory
        $count++;                   # keep a counter
    }
    close(FILE);
}
else {
    die "Could not open file $file: $!\n";
}

Rough template

There is a rough template of a perl script that creates/sets several useful variables (user, host, debug level) uses strict and -w, has USAGE information, ensures created files are accessible to others, processes options, has the disclaimer notice, etc. It is not meant to do anything useful but may be useful as a start.