Operating System - HP-UX
1753488 Members
4405 Online
108794 Solutions
New Discussion юеВ

Re: Perl - best practises

 
Tony Walker
Frequent Advisor

Perl - best practises

Hi Guys,

After intending to get into Perl for a while a problem at work demanded its use and so I have begun to use it. At present I'm only writing some basic stuff so have a few basic questions..

In the shell version of my script I would have a line such as

echo "$(date): $(hostname) has a problem" >> outfile

I want to do the same thing in perl.. I have seen people doing this by making a system call or using `date`. However, I was hoping to keep to JUST perl. Is it acceptable to still use 'shell' commands within your perl script or is there a better way?

Cheers,

Tony
5 REPLIES 5
Mark Grant
Honored Contributor

Re: Perl - best practises

Being pedantic about not using shell commands keeps things pure but in my view, the best tool for the job is what to use.

I love perl but it doesn't have a "lvdisplay -v vg00" command so I would quite happily use the shell to do that and so why not use the shell for more simple things too if it is actually easier. Having said that, extracting the date is very easy in perl and gives you individual variables that can be manipulated.

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

I remember when I started using perl that the temptation to just jump out to a shell for a quick "grep" was immense but it's not as flexible.

I would suggest, as a rule of thumb, do it with perl but if you know how to do it in shell and it has already taken half an hour to get it working in perl, use the shell.
Never preceed any demonstration with anything more predictive than "watch this"
Elmar P. Kolkman
Honored Contributor

Re: Perl - best practises

For the date command, I like to use strftime. U should add 'use POSIX qw(strftime)' in the header of the perl script, and then can do something like this:
printf "%s: %s has a problem\n",strftime('%d-%m-%Y %H:%M',gmtime(time)),ENV['HOSTNAME']);

Every problem has at least one solution. Only some solutions are harder to find.
H.Merijn Brand (procura
Honored Contributor

Re: Perl - best practises

1. Do in perl where Perl has builtins, like

my $now = locatime time;

2. Use modules for more advanced stuff (these are still pure perl (most of them), but offer a perl-style interface, and are mostly portable)

use Sys::Hostname;
use Time::gmtime;
use Time::HiRes;
use Time::Local;
use Time::localtime;
use Time::tm;

Using POSIX' strftime is easy, but slow.

There are modules for HP-UX specific environment, ALSO FOR LVM :)

ftp://download.xs4all.nl/pub/mirror/CPAN/modules/by-module/HPUX/HPUX-LVM_0.06.tar.gz

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Paddy_1
Valued Contributor

Re: Perl - best practises

Already people have answered your question.
Just to show some flexibility.
--------------------------
$time_text = localtime(time);
print $time_text, "\n"; # outputs whatever date/time it is

---------------------
time() will give "seconds since the epoch"
$today = time();
$tomorrow_at_this_time = time() + (24 * 60 * 60);
in case of date calculations
---------------------

Finally the statement
echo "$(date): $(hostname) has a problem" >> outfile

transaltes in perl as

open OUTFILE,"filename";
print OUTFILE "$time_text:$(hostname) has a problem"


The sufficiency of my merit is to know that my merit is NOT sufficient
H.Merijn Brand (procura
Honored Contributor

Re: Perl - best practises

Sorry Paddy. that's plain wrong. You open the file for *reading*

use Sys::Hostname
open OUTFILE, ">> outfile" or die "outfile: $!";
print OUTFILE scalar localtime, ": ", hostname, " has a problem\n";
close OUTFILE;

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn