Right, I would encourage you to post your full script, as rovf suggested, and let us know exactly what error you are having. What are $epoch_timestamp and $nowEpoc? Depending on how you're defining those variables, that may be part of your problem.
JavaFan certainly offers a simpler way to get what you're looking for (the script start time minus the file modification time, in days). For more info about the -M function, you can see the perlfunc manpage or here.
But, in terms of the way you're trying to do it:
I presume, also, that you're using File::stat to get the object-oriented interface to the stat function -- which, as JavaFan pointed out, returns a list without File::stat. (I like to use File::stat, too, since I can never remember what order stat returns things in.)
I am confused by what you're trying to do with time and localtime and $epoch_timestamp and all your other variables. This shouldn't be that complicated.
mtime from stat returns the file modification timestamp, in seconds since the epoch. The time function returns the current system time, in seconds since the epoch. localtime is used to split the seconds-since-the-epoch time from time into the current day, month, year, hour, second, etc. It also returns, in scalar context, the formatted ctime (e.g. Thu Oct 13 04:54:34 1994). You probably know all this.
I would simplify what you have above, like this:
#!/usr/bin/perl
use strict;
use warnings 'all';
use File::stat; # use the object-oriented interface to stat
my $file = "rajesh.txt";
my $file_timestamp = stat($file)->mtime; # gets the file-modified ti
+me, in
# seconds since the epoch
# my $startTime = time(); # You don't even really nee
+d this -- Perl has a
# built-in variable, $^T, t
+hat records when your
# script started to run. Se
+e the perlvar manpage.
my $timestamp = localtime($^T); # localtime, with no argume
+nt, assumes
# localtime(time). My examp
+le gets the localtime of $^T.
# Assigning this to a scala
+r variable, as
# you do here, gives you th
+e
# formatted ctime. You also
+ could
# have done localtime(time)
+ here,
# or just localtime with no
+ argument.
my $rajesh = $^T - $file_timestamp; # This is really all you ne
+ed to do here. You could
# also use the time functio
+n to get the current time,
# i.e. $rajesh = time - $fi
+le_timestamp
my $pretty_file_timestamp = localtime($file_timestamp); # Get the sca
+lar (ctime) format
print "The current time is = $timestamp\n";
print "The file modified time is = $pretty_file_timestamp\n";
print "The difference between file modified time and current time is =
+ $rajesh\n";
Some other things that might be worth knowing:
If you don't want to bother with creating a new variable, you can always force a scalar return from localtime, like so:
print "The current time is ", scalar(localtime), "\n";
Also, if you want to make even prettier time and date formats than ctime, e.g. "Thursday, February 23, 2012, 11:41 a.m.", then Date::Format, in the TimeDate bundle, is a handy module. |