1831593 Members
2909 Online
110027 Solutions
New Discussion

Perl quries - Newbie

 
SOLVED
Go to solution
yc_2
Regular Advisor

Perl quries - Newbie

Hi,

How to print out the hour, min and sec of time before and after sleep?

I tried the following but the output is the same:

#!/usr/bin/perl

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime time;
my $string="$hour$min$sec";
print "It is now $string\n";
sleep 15;
my $string_1="$hour$min$sec";
print "It is now $string_1\n";
8 REPLIES 8
Dennis Handly
Acclaimed Contributor
Solution

Re: Perl quries - Newbie

>I tried the following but the output is the same:

You need to invoke localtime again.
Note: If your $hour, $min or $sec only have one digit, your output will look funny without a separator.
James R. Ferguson
Acclaimed Contributor

Re: Perl quries - Newbie

Hi:

As Dennis noted, you never called 'localtime' a second time.

There are a number of ways to shorten and make your script more efficient.

Close to your original would be:

#!/usr/bin/perl
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime time;
printf "It is now %02d:%02d:%02d\n",$hour,$min,$sec;
sleep 15;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime time;
printf "It is now %02d:%02d:%02d\n",$hour,$min,$sec;


A more Perl-ish approach would be:

#!/usr/bin/perl
printf "It is now %02d:%02d:%02d\n", (localtime(time)) [2,1,0];
sleep 15;
printf "It is now %02d:%02d:%02d\n", (localtime(time)) [2,1,0];


Regards!

...JRF...
Ralph Grothe
Honored Contributor

Re: Perl quries - Newbie

Just another way making use of POSIX' strftime():


$ perl -MPOSIX=strftime -le 'print strftime("%T",localtime);sleep 15;print strftime("%T",localtime)'
08:42:25
08:42:40
Madness, thy name is system administration
yc_2
Regular Advisor

Re: Perl quries - Newbie

> printf "It is now %02d:%02d:%02d\n", (localtime(time)) [2,1,0];

If I want to assign to $string, how to go about it?
Ralph Grothe
Honored Contributor

Re: Perl quries - Newbie

> printf "It is now %02d:%02d:%02d\n", (localtime(time)) [2,1,0];
>
>If I want to assign to $string, how to go about it?

$time = join ':', (localtime)[2,1,0];
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor

Re: Perl quries - Newbie

Hi (again):

> If I want to assign to $string, how to go about it?

Yet another way:

#!/usr/bin/perl
my $string1 = sprintf "It is now %02d:%02d:%02d", (localtime(time)) [2,1,0];
sleep 15;
my $string2 = sprintf "It is now %02d:%02d:%02d", (localtime(time)) [2,1,0];
print "$string1\n$string2\n";

Regards!

...JRF...
yc_2
Regular Advisor

Re: Perl quries - Newbie

Thank you very much. Is there an URL where I can learn and raise my competent level for perl?
James R. Ferguson
Acclaimed Contributor

Re: Perl quries - Newbie

Hi (again(:

> Thank you very much. Is there an URL where I can learn and raise my competent level for perl?

The Perl community is a vast one. You might begin by following some of the links here:

http://www.perl.org/docs.html

In my opinion, the O'Reilly stable of books on Perl is the best supplement too.

By the way, if you are happy with the answers you have received, please read:

http://forums13.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...