On 10/03/2011 23:13, newbie01 perl wrote:
On Fri, Mar 11, 2011 at 6:19 AM, Rob Dixon <rob.di...@gmx.com
On 10/03/2011 15:33, newbie01 perl wrote:
Just want to know what improvement can be done with the code
below if any. I am sure a lot of improvement/changes is required.
I will be calling this script from a UNIX script,
i.e.timediff=`timediff.pl <http://timediff.pl> $file`, so $file
will be passed to the Perl script. It's probably a good idea to
check whether the $file is passed and if so, check if the file
exists or not.
Most of the print lines are just for testing/purposes.
Ultimately, what am wanting to assign to the timediff variable in
the UNIX script is the string "( 5 weeks, 3 days, 10:16:1 )" but
only if the file is x days old. Do I need to have something like
a return timediff_detail command in the Perl script?
FYI, I've decided to use stat instead of Perl Date modules
because each server have different Date modules installed, some
have Date::Manip, some have Date::Calc etc. so using stat is the
best option that I have.
Any advise/help will be much appreciated. Thanks.
-----
Code for timediff.pl <http://timediff.pl> so far
-----
#!/usr/bin/perl
#$today = localtime();
$today_epoch = time();
$today = localtime($today_epoch);
@date_fields = split(" ",$today);
$day=$date_fields[2];
$month=uc($date_fields[1]);
$year=$date_fields[4];
@time_fields = split(":",$date_fields[3]);
$hour=$time_fields[0];
$minute=$time_fields[1];
$second=$time_fields[2];
print "Today is ==> $today \n";
print "DAY is ==> $day ... \n";
print "MONTH is ==> $month ... \n";
print "YEAR is ==> $year ... \n";
print "HOUR is ==> $hour ... \n";
print "MINUTE is ==> $minute ... \n";
print "SECOND is ==> $second ... \n";
my ( $dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$atime, $mtime, $ctime,
$blksize, $blocks )
= stat("x1.pl <http://x1.pl>");
print "\n";
print "< ======================================> \n";
print "Output from stat ... \n";
print " dev => $dev \n";
print " ino => $ino \n";
print " mode => $mode \n";
print " nlink => $nlink \n";
print " uid => $uid \n";
print " gid => $gid \n";
print " rdev => $rdev \n";
print " size => $size \n";
print " atime => " . localtime($atime) . " ==> \n";
print " mtime => " . localtime($mtime) . " ==> \n";
print " mtime - not localtime => " . $mtime . " ==> \n";
print " ctime => " . localtime($ctime) . " ==> \n";
print " blksize => $blksize \n";
print " blocks => $blocks \n";
print "< ======================================> \n";
print "\n";
$difference=$today_epoch-$mtime;
$seconds = $difference % 60;
$difference = ($difference - $seconds) / 60;
$minutes = $difference % 60;
$difference = ($difference - $minutes) / 60;
$hours = $difference % 24;
$difference = ($difference - $hours) / 24;
$days = $difference % 7;
$weeks = ($difference - $days) / 7;
print "Age of x1.pl <http://x1.pl> is: \n";
print "( $weeks weeks, $days days, $hours:$minutes:$seconds ) \n";
print "\n";
exit 0;
Sample Output:
[snip]
Age of x1.pl <http://x1.pl> is:
( 5 weeks, 3 days, 10:16:1 )
You must always 'use strict; use warnings;' at the start of any Perl
program. They will make it much easier to find most trivial bugs.
It would simplify things a lot if you used the -M operator to establish
the age of the file. See 'perldoc -f -X'.
The program below does what you want.
use strict;
use warnings;
my $file = 'x1.pl';
my $age = -M $file;
my $d = int $age;
$age = ($age - $d) * 24;
my $h = int $age;
$age = ($age - $h) * 60;
my $m = int $age;
$age = ($age - $m) * 60;
my $s = int $age;
my $w = int($d / 7);
$d %= 7;
printf "Age of %s is:\n( %d weeks, %d days, %d:%02d:%02d )\n",
$file, $w, $d, $h, $m, $s;
__END__
Thanks a lot for the code ... am embarrassed on how many lines you've
cut it down to :-)
(Please bottom-post your responses on this list. Thanks.)
That's fine - I have saved a lot of code by using -M instead of stat(),
and your code had many lines of comments and diagnostics whereas mine
had none :)
I can use warn with the -M right in case the filename supplied as an
argument does not exist?
-M just returns undef if the file doesn't exist - it doesn't throw a
warning. So you can write
my $age = -M $file or die "Unable to establish age of $file: $!";
I'm afraid my home isn't on a Unix system so I can't help with how to
get the string out to a shell script variable, but that should be
trivial. Unfortunately I can't comment either on whether this is the
best solution, but I am sure others can help.
By the way, if you release your program together with a Perl module in
the same directory it will run fine, although it will have to be a
Perl-only module to ensure that it runs on a variety of platforms.
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/