Operating System - HP-UX
1822551 Members
2992 Online
109642 Solutions
New Discussion юеВ

Perl's -M file Test operator

 
SOLVED
Go to solution
Daavid Turnbull
Frequent Advisor

Perl's -M file Test operator

This should be a snap so I must be doing something really dumb.

I want to do something with files created in the last X days. (I have used 17 in this example to pick up files from this month.)

For the directory in question there is a bunch of matching files going back years.

Why when I execute this code is the "false" line aways printed???
____________

#! /usr/bin/perl
$aDir = '/tmp';

@result = `find $aDir -name \"*error.log\"`;

foreach $file (@result) {

if ((-M $file) > 16.0)
{ $tmp = `ls -l $file`; print "true: $tmp"; }
else
{ $tmp = `ls -l $file`; print "false: $tmp"; }
}
Behold the turtle for he makes not progress unless he pokes his head out.
8 REPLIES 8
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl's -M file Test operator

Here's the perl way to do it

lt09:/home/merijn/itrc 128 > perl -MFile::Find -le'find(sub{/\.log$/ and$res{$File::Find::name}=[stat]},"/tmp");for(keys%res){$x=-M$_<16.0?"True":"False";print scalar localtime$res{$_}[9]," $_ ($x)"}'
Tue Mar 22 14:16:04 2005 /tmp/kde-merijn/konqueror-crash-o4ueGb.log (False)
Sun May 8 12:05:28 2005 /tmp/p59.log (True)
Sun May 1 18:33:50 2005 /tmp/xine-plugin.log (True)
Sun May 1 18:33:42 2005 /tmp/Download/hs_err_pid25962.log (True)
Thu Dec 16 18:53:44 2004 /tmp/Download/gnokii-0.6.4/config.log (False)
Tue May 3 23:45:29 2005 /tmp/kde-merijn/konqueror-crash-9qeahb.log (True)
Thu May 12 23:24:43 2005 /tmp/kde-merijn/konqueror-crash-J39zLb.log (True)
Tue Mar 22 19:36:35 2005 /tmp/kde-merijn/konqueror-crash-6ugIvb.log (False)
Tue Apr 19 19:02:41 2005 /tmp/kde-merijn/konqueror-crash-AVMgqb.log (False)
Tue May 3 23:51:23 2005 /tmp/kde-merijn/konqueror-crash-YA25Xa.log (True)
Tue Apr 19 14:04:19 2005 /tmp/kde-merijn/konqueror-crash-USJMgc.log (False)
Fri Apr 22 20:41:55 2005 /tmp/kde-merijn/konqueror-crash-zP78wb.log (False)
Mon Oct 13 20:40:06 2003 /tmp/Download/avifile-0.7-0.7.38/config.log (False)
Tue May 3 20:13:38 2005 /tmp/kde-merijn/konqueror-crash-IILVNb.log (True)
lt09:/home/merijn/itrc 129 >

Or in a script:

--8<---
use strict;
use warnings;
use File::Find;

my %res;
find (sub {
m/\.log$/ and $res{$File::Find::name} = [ stat ];
},"/tmp");

foreach my $file (keys %res) {
my $x = -M $_ < 16.0 ? "True" :"False";
print scalar localtime $res{$_}[9], " $_ ($x)\n";
}'
-->8---

Sorry I switched true and false, but I guess you get the drift here

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Daavid Turnbull
Frequent Advisor

Re: Perl's -M file Test operator

Yes H.Merijn, this does look like it will do what I am after - thank you.

I will update just how well it works tomorrow before closing the thread and assigning points.

I am curious however to know what was wrong with my code? (apart from relying to much on commands designed for ues in a shell.) The relevant pages of may camel book are quite warn!
Behold the turtle for he makes not progress unless he pokes his head out.
H.Merijn Brand (procura
Honored Contributor

Re: Perl's -M file Test operator

OK, I'll tell you if you promise not to use external `find`s anymore :)

If you take a real close look at the entries in @result, you'd see that the file names have trailing new lines :)

chomp (@result = grep /error\.log$/ => `find /tmp`);

would have done it your way

Enjoy, Have FUN! H.Merijn

Enjoy, Have FUN! H.Merijn
Ermin Borovac
Honored Contributor

Re: Perl's -M file Test operator

I think the reason why your code didn't work is that you forgot to chomp the newline character on $file variable.

foreach $file (@result) {

chomp $file;

...
}
Daavid Turnbull
Frequent Advisor

Re: Perl's -M file Test operator

Thanks! Once I unraveled what each bit did it makes sense and was easy to import into the application.

"warnings.pm" does not seem to be available on the system I am working on which seemed a little odd. I presume this is normally part of the standard distribution?
Behold the turtle for he makes not progress unless he pokes his head out.
Ermin Borovac
Honored Contributor

Re: Perl's -M file Test operator

warnings.pm is core module that was first introduced in 5.6 me thinks.

It is a replacement for command line flag -w, and while -w flag is global, warnings.pm can be used to enable/disable warnings for a block of code.
H.Merijn Brand (procura
Honored Contributor

Re: Perl's -M file Test operator

If you do not have warnings, you run 5.6.2 or older. You could consider upgrading to a more recent perl:

warnings was first released with perl 5.006
5.006 undef
5.006001 undef
5.006002 undef
5.007003 1.00
5.008 1.00
5.008001 1.03
5.008002 1.03
5.008003 1.03
5.008004 1.03
5.008005 1.03
5.008006 1.03
5.009 1.03
5.009001 1.03
5.009002 1.04

The big advantage of 'use warnings' over -w, is that use warnings is lexically scoped. That means that if you use -w (which is good), it would also promote the warnings setting to all modules and scripts used from the calling process, whereas 'use warnings' only affects the current scope, and you can even put off subsets of warnings for (sub) blocks where you know things would warn.

# perldoc warnings
# perldoc perllexwarn

for a more elaborate explanation and examples that make sense. The latter also describes wich categories can be enabled or disabled.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Daavid Turnbull
Frequent Advisor

Re: Perl's -M file Test operator

Thanks for your assistance.

I have learnt a lot from your input.

I promise not to use the external find from within Perl ;-)

(I am a tad embarrased about not picking the new lines as the culprit for why my original code was not working as I have been caught by this previously.)

The current platform is perl5.005_03 which explains why warnings.pm was not present.
Behold the turtle for he makes not progress unless he pokes his head out.