Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Perl File-Test Operators

by gyasuda (Initiate)
on Aug 11, 2006 at 02:07 UTC ( #566734=perlquestion: print w/replies, xml ) Need Help??
gyasuda has asked for the wisdom of the Perl Monks concerning the following question:

I am using an AIX 5200-05 (IBM UNIX) with perl.rte

I have a file /tmp/log/startOnline.was_20050801_0701.joblog which has a timestamp of Aug 01 2005. The last time it was accessed and modified was like over 370 days. Of course, the /tmp/log directory has many log files.

In Perl, here is part of the code:

$path = "/tmp/log"; $file = "startOnline.was_*_*.joblog"; @TODELETE = `ls -t $path/$file`; foreach $logfile (@TODELETE) { printf ("%12s %5d %5d %5d\n", $logfile, -M $logfile, -A $logfile, -C + $logfile); }

The output lists "/tmp/log/startOnline.was_DATE_TIME.joblog 0 0 0".

In Perl, I thought the "-M" file-test operator returns how long since the file has been modified, "-A" returns how long since the file has been accessed, and "-C" returns how long since the file's inode has been accessed.

If $logfile has the full path of the filename, how come the file-test operators display zeroes?

If I can get the file operators to work, I can finish my Perl script to start deleting the older log files.

Any help would be appreciated.


Replies are listed 'Best First'.
Re: Perl File-Test Operators
by ikegami (Pope) on Aug 11, 2006 at 02:12 UTC

    You forgot to remove the trailing newlines from ls's output. IIRC, using warnings would have told you as much.

    Warnings would also have told you you were trying to print undef, not 0, in which case you should have checked the error message in $!.

Re: Perl File-Test Operators
by graff (Chancellor) on Aug 11, 2006 at 03:08 UTC
    The point about the -M, -A and -C args being "in days" is that these are float values, not integers. You might want your printf format to use something like "%7.2f" rather than "%5d" for these values.

    If you read "perldoc -f -X", you'll notice that you can save a few ops by using underscore "_" when doing a second (third, ...) look-up of a stat value on the same file:

    printf ("%12s %7.2f %7.2f %7.2f\n", $loglfile, -M $logfile, -A _, -C + _);

    Also, the idea of using glob is very good (avoids the need to worry about newlines at the ends of the file names); the idea about using File::Find is probably not worth the trouble.

    (updated code snippet to use "%7.2f", as per my own suggestion)

Re: Perl File-Test Operators
by jwkrahn (Monsignor) on Aug 11, 2006 at 02:33 UTC
    Instead of running ls in a separate process you should use Perl's built-in glob operator:
    foreach my $logfile ( <$path/$file> ) {
Re: Perl File-Test Operators
by GrandFather (Sage) on Aug 11, 2006 at 02:17 UTC

    From perlfunc:

    -M Script start time minus file modification time, in days.
    -A Same for access time.
    -C Same for inode change time (Unix, may differ for other platforms)

    Note: in days.

    DWIM is Perl's answer to Gödel
Re: Perl File-Test Operators
by ptum (Priest) on Aug 11, 2006 at 02:18 UTC

    You'll want to chomp the $logfile within your foreach.

    Update: It wouldn't hurt you to use strict, either.

    Another Update: You might consider using File::Find to select the log files you want.

    No good deed goes unpunished. -- (attributed to) Oscar Wilde

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://566734]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-05-26 03:01 GMT
Find Nodes?
    Voting Booth?