Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

File::Find Woes

by Ovid (Cardinal)
on Oct 25, 2000 at 03:48 UTC ( [id://38258]=perlquestion: print w/replies, xml ) Need Help??

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

I have some code that needs to do stuff to many files. It uses File::Find to search directories recursively from the current directory. Here's a minimal test case that gives me the problem:
#!D:/perl/bin/perl.exe -w use strict; use File::Find; my @list = qw(.); find ( \&recurse, @list ); sub recurse { my $file = $File::Find::name; productionize ( $file ); } sub productionize { my $fileName = shift; return if $fileName !~ /[\w]+\.(?:cgi|p[lm])$/; print "Processing $fileName\n"; open FILE, "<$fileName" or die "Can't open $fileName for reading: +$!"; #do stuff close FILE; }
It displays the following:
Processing ./blobtest.pl Processing ./form.pl Processing ./functional.pl Processing ./images.cgi Processing ./index.pl Processing ./upload.cgi Processing ./utils/debug.pl Can't open ./utils/debug.pl for reading: No such file or directory at +correct.pl line 17.
As soon as I try to descend into a sub directory, it chokes. I keep reading through the documentation and I assume that I'm overlooking something simple. What am I missing?

NT 4.0, service pack 6. ActiveState Perl v5.6.0.

Humbly yours,
Ovid

P.S. For those who might wonder why I've bothered to call productionize() from recurse(): the code is actually considerably more complicated. I simply have a very simple test case here.

Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

Replies are listed 'Best First'.
Re: File::Find Woes
by japhy (Canon) on Oct 25, 2000 at 03:53 UTC
    Because File::Find::find() will chdir() into the directory where the file is. So don't try opening $File::Find::name, but rather, just $_

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
Re: File::Find Woes
by runrig (Abbot) on Oct 25, 2000 at 03:54 UTC
    File::Find chdir's to the subdirectory, and you are opening './subdir/file' from the subdirectory. You just need to open '$_', the current filename, since you are in the subdirectory.

    And so, BTW, you might want to change this line:
    return if $fileName !~ /[\w]+\.(?:cgi|p[lm])$/;
    To this:
    return unless /^\w+\.(?:cgi|p[lm])$/;
Re: File::Find Woes
by d_i_r_t_y (Monk) on Oct 25, 2000 at 08:30 UTC
    i was bitten by this File::Find-chdir bug not so long ago... although i notice that you are passing productionize the value of $File::Find::name, which, according to the perl5.6 docs says:
    `$File::Find::name' contains the complete pathname to the file. You are chdir()'d to `$File::Find::dir' when the function is called, unless `no_chdir' was specified. When

    ...in which case you should have the correct pathname in productionize. i don't believe this was the case with previous versions(???).

    you may want to check out the no_chdir option (from man File::Find...):

    `no_chdir' Does not `chdir()' to each directory as it recurses. The wanted() function will need to be aware of this, of course. In this case, `$_' will be the same as `$File::Find::name'.

    personally, in the future i would stick with defining my own routines for the current path and file, ie:

    sub current_dir { $File::find::dir } sub current_file { $_ }
    ...and then go from there... just in case you end up writing your own traversal function and ditching File::Find altogether... ;-)

    paranoid dirty

RE: File::Find Woes
by OzzyOsbourne (Chaplain) on Oct 25, 2000 at 15:18 UTC

    I had this problem on this post and haven't had time to do an in depth look at it, but what I did find was this: NT and 2000 have character limits in the directory name. I believe that the limit is 255, but I'm not sure. When my script gets to a directory path over 255, it chokes, too. We had been talking about a mod to file::find, or setting a variable from it, but I never got an answer. The thread is here.

    -OzzyOsbourne

RE: File::Find Woes
by OzzyOsbourne (Chaplain) on Oct 25, 2000 at 15:18 UTC

    This dupe was deleted...sorry.

(Ovid) Re: File::Find Woes
by Anonymous Monk on Oct 25, 2000 at 21:51 UTC
    Hey, thanks for all of the help! I appreciate it.

    Cheers, Ovid

Log In?
Username:
Password:

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

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

    No recent polls found