# perl -MCPAN -e 'shell' cpan[#]> install Net::SMTP cpan[#]> install CPAN cpan[#]> reload CPAN cpan[#]> quit
# perl -MCPAN -e 'shell' cpan[#]> install Net::SMTP cpan[#]> install CPAN cpan[#]> reload CPAN cpan[#]> quit
Opening a file handle in Perl for standard out? Try open(FILEHNDL, ">> -");
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:
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.
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]).
#!/usr/bin/perl 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]); $year+=1900; $mon+=1; print "It is now: $mon/$mday/$year $hour:$min DST: $DST[$isdst]\n"; geen@wwwgeen:~/dev/src/pl> ./test.pl It is now: 11/6/2013 13:21 DST: No
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 shell=/bin/bash #Sample split statement in perl whie ( my $record,<$INFILE> ) { my($keyname,$valueinfo)=split(/\s*=\s*/, $record); }