Perl Script: Basic Template

[code language=”perl”] #! perl #=============================================================================== # Objective: # ———- # # This is a sample Perl script template. # #=============================================================================== # Include Modules #=============================================================================== use strict; use warnings; use Pod::Usage; use Getopt::Long qw(:config no_ignore_case bundling); #=============================================================================== # Global Variables Declaration #=============================================================================== use vars qw($DEBUG); #=============================================================================== # Prototypes Section #=============================================================================== sub DoAction; sub InitGlobals; sub ProcessArgs; #=============================================================================== # main() #=============================================================================== { InitGlobals(); ProcessArgs(); DoAction(); } #=============================================================================== # sub InitGlobals #=============================================================================== sub InitGlobals { } #=============================================================================== # sub ProcessArgs #=============================================================================== sub ProcessArgs { Getopt::Long::Configure("bundling", "no_ignore_case"); if (!GetOptions(‘D’ => \$DEBUG, ‘h|?’ => sub { &pod2usage(-verbose => 2)}) || @ARGV ) { pod2usage(2); } […]

Read more

Perl Script Template with basic logging functions

[code language=”perl”] #! perl #=============================================================================== # Objective: # ———- # # Sample Perl script template with basic logging functions # # # $Header: $ #=============================================================================== # Include Modules #=============================================================================== use strict; use warnings; use Pod::Usage; use Getopt::Long qw(:config no_ignore_case bundling); #=============================================================================== # Global Variables Declaration #=============================================================================== use vars qw($DEBUG); #=============================================================================== # Prototypes Section #=============================================================================== sub DoAction; sub InitGlobals; sub ProcessArgs; sub Info {my ($mesg) = @_; print "INFO: $mesg\n";} sub MyErr {my ($mesg) = @_; print "ERROR: $mesg\n"} sub MyWarn {my ($mesg) = @_; print "WARNING: $mesg\n";} sub MyDie {my ($mesg) = @_; print "ERROR: $mesg\n"; exit(1);} sub Debug {my […]

Read more

Perl: Using /g modifier in scalar context

When used in scalar context, the /g modifier indicates a progressive match, which makes Perl start the next match on the same variable at a position just past where the last one stopped. After a failure, the match position normally resets back to the start. Example: [code language=”perl”] C:\>perl $a = "test"; ($a =~ /test/g) ? print "Found!\n" : print "Missing!\n"; # Check again … the fun starts here ($a =~ /test/g) ? print "Found again!!\n" : print "Missing again!!\n"; # Check again … the fun starts here ($a =~ /test/g) ? print "Found after failure \n" : print "Missing […]

Read more