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

Perl script: Auto Detect the script file name

CODE: [code language=”perl”] use File::Basename; my $progname = basename($0); $progname =~ m/(.*)(\.)(.*)/; print "Script name along with extension: [$progname]\n"; print "Script name without extension: [$1]\n"; [/code] OUTPUT: [code language=”perl”] C:\>perl get-script-name.pl Script name along with extension: [get-script-name.pl] Script name without extension: [get-script-name] C:\> [/code]

Read more