rajeshatbuzz has asked for the wisdom of the Perl Monks concerning the following question:

I am writing one script which will compare file modification time and current time and if both has more than 24 hours diff, then print some message somthing like this file is older than 24 hours. I have written following script bt stuck on d way..any help?

my $file = "rajesh.txt"; my $file_timestamp = stat($file)->mtime; my $timestamp = localtime($epoch_timestamp); my $startTime = time(); my $rajesh = $nowEpoc - $file_timestamp; print "The current time is = $startTime\n"; print "The file modified time is = $file_timestamp\n"; print "The difference between file modified time and current time is = + $rajesh\n";

Replies are listed 'Best First'.
Re: compare file modification time and current time
by JavaFan (Canon) on Feb 23, 2012 at 11:34 UTC
    stat does not return an object, but a list. To get the mtime from stat, use (stat $file)[9].

    But you can do away with stat altogether:

    if (-M "rajesh.txt" >= 1) { print "The file was modified more than 1 day ago\n"; }
      stat does not return an object, but a list.
      I guess the OP posted only part of his program, and s/he left out use File::stat;, which would cause stat to return an object. Otherwise s/he would have gotten an error message about that.

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: compare file modification time and current time
by LonelyPilgrim (Beadle) on Feb 23, 2012 at 17:33 UTC

    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.

Re: compare file modification time and current time
by rovf (Priest) on Feb 23, 2012 at 12:27 UTC
    stuck on d way.

    What does that mean? Does the devil crawl out of your PC when you run the script? Do you get an "out-of-memory" error? Sorry, mate, but you have to be more explicit when you ask for help.

    -- 
    Ronald Fischer <ynnor@mm.st>