Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do I use stat

by bgator29 (Initiate)
on Sep 27, 2001 at 20:44 UTC ( [id://115152]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am having a terrible time with the stat function. I am trying to add code to a current program to where it will print out an error message if the last file read is older than 20 minutes. I am trying to get it in seconds so (1200 seconds). I have been to CPAN and looked at stat, but I guess I'm not following it correctly. Here is what I have:
my @array = ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $ati +me, $mtime, $ctime, $blksize, $block); @array = stat($_); $array = @array[8]; if($array > 1200){ print "ERROR MESSAGE";}
ANY HELP is greatly appreciated...........THank YOu!

Edit by tye

Replies are listed 'Best First'.
Re: How do I use stat
by ducky (Scribe) on Sep 27, 2001 at 20:52 UTC

    Try <code> and </code> tags =)

    Close. stat's time functions give you the times since the epoch so you need to figure out how many seconds it's been. You should be able to get away with this:

    if ( time - (stat $_)[9] > 1200 ) { print "ERROR MESSAGE" }
    This gets rid of the variables I'm guessing you're not using, as well.

    Let me know if that works for ya. =)

    -Ducky

      thanks for the tips. I'm new to PERL ( you could probably tell). That really got me off to a great start.........one more question though please. Aside from the code that you gave me, do I have to have anything else because i think that the (stat $_) is not actually working. Do I need to have any code before i make the if comparison?? Hopefully that makes sense Thanks a lot

        No problem! That is exactly why we're here =)

        One thing to consider when doing stat is that it operates off of the current working directory. Perhaps $_ isn't really pointing to a valid file? Proper error checking is very good practice and often needs very little extra modification:

        die "Bigger problem: '$_' doesn't exist!" if not -e $_ ; if ( time - (stat $_)[9] > 1200 ) { print "ERROR MESSAGE" }
        Oh, and no, stat works just fine by its lonesome. No prep is needed.

        -Ducky

Re: How do I use stat
by jeroenes (Priest) on Sep 27, 2001 at 20:55 UTC
    die "Argghh" if -M $_ > 1/24/3; If $_ holds the name of the file you want to check. Look at perlfunc for -M, it gives you the change time in days. See also $ARGV in perlvar.
Re: How do I use stat
by tommyw (Hermit) on Sep 27, 2001 at 20:51 UTC
    The time fields are absolute times (so, for example, they represent 12:03 today). You need to compare them to the current time, available (amazingly) from the time function.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://115152]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found