Geen Family Web Site


Install Perl Module

How to add Perl modules to your Linux installation. As root, execute the following commands. First enter the Perl CPAN shell. Then, once in the shell install the module you want, in this example I chose Net::SMTP. Check for newer version of the CPAN and reload the CPAN. When yoa re done, quit.
# perl -MCPAN -e 'shell'
cpan[#]> install Net::SMTP
cpan[#]> install CPAN
cpan[#]> reload CPAN
cpan[#]> quit

Open standard out

Opening a file handle in Perl for standard out? Try open(FILEHNDL, ">> -");

Perl Help function

I try to add hlep output to all of my perl programs to help users who are unfamiliar with the program. To do this, I assign the help string to a global variable and then I can reference the string with the print command. Here is how I setup the global variable:

$0 [-o option list] {-x value1 | -y value2} -r requiredinfo
enter some descriptive text here.
print "$::help";

Perl case statement

There was a time when perl did not have its own case/switch statement. For that reason you will find the following type of code in most of my programs. It is a simple command line parser that seems to work well.

# If no arguments are passed to to the program then print the "HELP".
if (! @ARGV ) { print "$::help"; exit 0; }
while(@ARGV) #loop through until the ARGV array stack is empty.
#setup a temp variable to hold the next arguemnt on the ARGV stack.
my $cmdparm = shift(@ARGV);
ARGVSW: #create a LABEL to loop back to.
#test each command line parameter and then perform a task based on a successful match.
#the last statement breaks out of the internal block, ARGVSW:
if ($cmdparm eq "-f") {$firstname = shift(@ARGV); last; }
if ($cmdparm eq "-l") {$lastname = shift(@ARGV); last; }
if ($cmdparm eq "-e") {$email = shift(@ARGV); last; }
if ($cmdparm eq "-u") {$loginname = shift(@ARGV); last; }
if ($cmdparm eq "-r") {$region = shift(@ARGV); last; }
if ($cmdparm eq "-j") {$jobfunc = shift(@ARGV); last; }
if ($cmdparm eq "-c") {$contract = shift(@ARGV); last; }
if ($cmdparm eq "-h") {print "$::help"; exit 0; }
#If an invalid parameter is passed on the command line, then print the help.
print "$::help!\n"; exit 0;
} #end ARGVSW
} #end while command line parameters


Perl has a built in function to retrieve the system date and time. The localtime function returns an array of nine (9) values but it is rare that you are interested in all nine values. There is a simple way to just retrive the values of interest by treating them as they are, an array.

First we set up the scalar variables we need to receive the values from the localtime function. In this case we are collecting minutes, hour, day of month, month, year, and if the Daylight Saving Time bit is set. We put them in parentheses to treat them as input into, or in this case output from, an array my ($min, $hour, $mday, $mon, $year, $isdst).

Next we query the system for the time and evaluate it as local time. (localtime(time())). Since we only some of the values we only reference those values from the array [1,2,3,4,5,8]. In order to make the two sides equal, we must eclose the entire expression in parentheses ((localtime(time()))[1,2,3,4,5,8]). It may be possible to write this expression in shorthand but this is the most explicet representation.

It may be possible to drop the time() function making the local time function look like ((localtime())[1,2,3,4,5,8]) or even ((localtime)[1,2,3,4,5,8]).

my @DST = ("No", "Yes");
#get system time but just the minutes, hour, day, month, year, and Daylight Saving Time bit.
my ($min, $hour, $mday, $mon, $year, $isdst)=((localtime(time()))[1,2,3,4,5,8]);
print "It is now: $mon/$mday/$year $hour:$min DST: $DST[$isdst]\n";

geen@wwwgeen:~/dev/src/pl> ./
It is now: 11/6/2013 13:21 DST: No

split fuction

Sometimes you may want read configuration data from a file. This is fine but you cannot always control the format data is entered. It is, therefore, a good idea to provide your end user a little wiggle room. Let us assume that we have a configuration file with a key = value format. On the left side of the equal sign we have a key such as home directory. On the right side we have the value to be stored with with the key such as /home/geen. A simple way to read

In the following example I have an input file that has a key/vale pair with a separator of and equal sign (=). If you note closely, the spaces are inconsistant. Sometimes there is a space before and/or after the equal sign. To account for this we just use a regular expression to note that there may be zero or more white spaces around the equal sign. We do this with the \s* regular expression notation.

#input file
userid = geen
homedir =/home/geen

#Sample split statement in perl
whie ( my $record,<$INFILE> )
	my($keyname,$valueinfo)=split(/\s*=\s*/, $record);

Par Packer

To compile a perl program first install the PAR::Packer moduel
perl -MCPAN -e "install PAR::Packer"

Then, to compile the program into executable format
pp -o <DesiredExeName>.exe <MyFancyPerlScript>

My available downloads