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

here is my code:
use File::Find; find (\&wanted, "./categories/"); sub wanted { $m = -M "$File::Find::name"; print "M = $m <br>"; print<<ENDHTML $File::Find::name<br><br> ENDHTML if (-M $File::Find::name >= 7){ unlink $File::Find::name; } } # End
it prints the filname ok, but never prints $m

sometimes i use File::Find in a temporary script in my home directory, and it never finds the date for alot of files.. it will find a date for the file "." and other weird things like that... but i can never get everything to work fine.
anyone have any idea?

2001-06-16 Edit by Corion : Changed title

Replies are listed 'Best First'.
Re: -M thing
by stephen (Priest) on Jun 11, 2001 at 22:33 UTC

    You need to use -M $_ rather than -M File::Find::name.

    Your code is printing $m. It's just that $m is empty, because -M $File::Find::name can't find the file. File::Find goes recursively through directories and chdirs into each one it encounters. $_ is the filename relative to the current directory. $File::Find::name is the filename relative to the directory you started in (i.e. the one you ran the script in).

    stephen

Re: -M thing
by lemming (Priest) on Jun 11, 2001 at 22:36 UTC
    Try -M File::Basename::basename("$File::Find::name","");
    The reason your code didn't pick up the modification is because File::Find is in the directory "categories"
Re: -M thing
by suaveant (Parson) on Jun 11, 2001 at 22:29 UTC
    I tried this code... the only way that made it work was to give the full path to -M... I can't say why... but that fixes it... explains why . and .. work, since they are defined no matter what dir you start in...

    Update ahhh, stephen explains well why this is true, makes sense too! :) ++

                    - Ant