Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

How do I use stat

by bgator29 (Initiate)
on Sep 27, 2001 at 20:44 UTC ( #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. =)


      thanks for the tips. I'm new to PERL ( you could probably tell). That really got me off to a great 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.


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://115152]
Approved by root
[ovedpo15]: can't I use just use lib $FindBin::Bin/../ bin" asuming that p2 is in ./bin and p1 is in /config? it says "cant find" altought p2 is
[moritz]: ovedpo15 "use lib" is only for .pm files
[moritz]: if you don't have .pm file, you might to use "require" or "do" with an absolute path

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-03-17 20:43 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (226 votes). Check out past polls.