Perl Modules and My Functions that I use over and over again
Published by Alphamonk on 2009/2/21 (960 reads)
I use a certain modules on most of the scripts that I write for automation. I will name those modules and explain why and how I use them. I will show you templates of some of my most often used subroutines in this article too.
use strict;
The first pragma we will go over is strict. Perl is very forgiving when it
comes to variables. Using strict reminds us to declare variables, before
initializing them. This will help keep variables local when using modules too.
use warnings; or better yet #!/usr/bin/perl -w;
Turning on warnings will aid you in understanding why and when things do go as planned. These two pragmas are necessary for every Perl script you write. It may be easier for you to write a working program without them. But you will become a better programmer when you write programs with using them.
use Getopt::Long;
When I write a program that is able to satisfy the usage needs of more than one user, with the difference in use being the arguments or parameters given to the program, I use Getopt::Long. I return my command line variable near the top of my program.
The usage function below is what I alway use with Getopt:Long. I use this function to ensure competing arguments can not be used and to show examples of the correct to use command line parameters with the program.
This is very simple function to run commands. I don't actually use this function but I found it in my bin/perl directory. I most likely wrote this in 1999, it does work. I just wanted to show you a run command function
Some of the modules that need to be added to this list are:
Cwd; # Get pathname of current working directory
File::Copy; # Copy files or filehandles
File::Basename; #Parse file paths into directory, filename and suffix.
IPC::Cmd; # Running system commands made easy
Config::IniFiles; # Reading and writing to config files
File::stat; # Parse file information
File::Find; # Traverse a directory tree
File::Path; # Create or remove directory trees
I use these all the time. All the above are core modules except Config::Inifiles. I use a subroutine that I wrote for Run Cmd but I wanted to let you know about IPC::Cmd. IPC::Cmd was difficult to use the last time I tried using it, which was many years ago.
use strict;
The first pragma we will go over is strict. Perl is very forgiving when it
comes to variables. Using strict reminds us to declare variables, before
initializing them. This will help keep variables local when using modules too.
use warnings; or better yet #!/usr/bin/perl -w;
Turning on warnings will aid you in understanding why and when things do go as planned. These two pragmas are necessary for every Perl script you write. It may be easier for you to write a working program without them. But you will become a better programmer when you write programs with using them.
use Getopt::Long;
When I write a program that is able to satisfy the usage needs of more than one user, with the difference in use being the arguments or parameters given to the program, I use Getopt::Long. I return my command line variable near the top of my program.
my @cmdlineargs = my($arch,$interactive,$bg,$local,$project,$priority,$mem,
$queue,$tmpfree,$slots,$hipriority,$medpriority,$lowpriority,
$Submit,$notify
) = GetCmdLine()
);
sub GetCmdLine{
my ($ok,$help);
$ok = GetOptions(
"arch=s" => $arch,
"I" => $interactive,
"bg" => $bg,
"L" => $local,
"project=s" => $project,
"priority=s" => $priority,
"highpriority" => $hipriority,
"medpriority" => $medpriority,
"lowpriority" => $lowpriority,
"mem=s" => $mem,
"que=s" => $queue,
"tmpfree=s" => $tmpfree,
"notify" => $notify,
"help" => $help
);
Usage() if($help);
Usage() if(!($arch == 32 || $arch == 64));
Usage() if($interactive && $bg);
Usage() unless(
$arch ||
$interactive ||
$bg ||
$local ||
$project ||
$priority ||
$mem ||
$queue ||
$tmpfree ||
$slots ||
$Submit ||
$notify); return($arch,$interactive,$bg,$local,$project,$priority,
$mem,$queue,$tmpfree,$slots,$hipriority,$medpriority,
$lowpriority,$Submit,$notify
);
}
The usage function below is what I alway use with Getopt:Long. I use this function to ensure competing arguments can not be used and to show examples of the correct to use command line parameters with the program.
sub Usage{
print <<USAGE;
Syntax: sgesubmit.pl [options]
--arch : Cpu type options are 32 and 64 only
--I : Run in interactive mode
--bg : Run the job in the background
--L : Run in local mode
NOTE: Only these exact branch names are allowed:
be2 and/or fpga
--project : What project to run job in
--priority: Priority number -10 highest 10 lowest
--highpriority : Jobs run with high priority
--medpriority : Jobs run with normal priority
--lowpriority : Jobs run with low priority
--mem : Memory requirement for the job if known
--que : What que the job should run in
--project : What project the job should run in
--tmpfree : How much free tmp space the job requires
--notify : Mail the results
--help : Prints this help message
Example : sgesubmit.pl --arch 32 --I --mem 2G --notify
USAGE
exit;
}
This is very simple function to run commands. I don't actually use this function but I found it in my bin/perl directory. I most likely wrote this in 1999, it does work. I just wanted to show you a run command function
sub run_cmd {
my ($cmd) = @_;
print "$cmdn";
system($cmd);
}
Some of the modules that need to be added to this list are:
Cwd; # Get pathname of current working directory
File::Copy; # Copy files or filehandles
File::Basename; #Parse file paths into directory, filename and suffix.
IPC::Cmd; # Running system commands made easy
Config::IniFiles; # Reading and writing to config files
File::stat; # Parse file information
File::Find; # Traverse a directory tree
File::Path; # Create or remove directory trees
I use these all the time. All the above are core modules except Config::Inifiles. I use a subroutine that I wrote for Run Cmd but I wanted to let you know about IPC::Cmd. IPC::Cmd was difficult to use the last time I tried using it, which was many years ago.
| Navigate through the articles | |
Perl Automation Script using a config file
|
Very simple automation template
|
Voters total: 0
Average: 0
|
The comments are owned by the poster. We aren't responsible for their content.
|


