Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

File::Finder usage

by TheMarty (Acolyte)
on Oct 29, 2004 at 11:38 UTC ( #403691=perlquestion: print w/ replies, xml ) Need Help??
TheMarty has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Maybe I'm too stupid to understand the documentation, and maybe examples in the docu are so complex, and I need simple thing. I'm trying to find some specific file using File::Finder. Something like: myfile.txt I do not know in which directory is that file. In case I find it, I'd like to get the full path to it. Other case - undef (or 0). I managed to get the full list of all files with File::Finder
sub FindFile { my $file = shift(@_); my @directories = @_; my $pathtofile; my @allfiles; unshift(@directories,"./"); @allfiles = File::Finder->in(@directories); return $pathtofile; };
I tried to use File::Finder->name($file)->in(@directories) but this gives me only 1 or 0 (if file is there or not). Of course, when I have this big @allfiles list, I can use foreach and go through and check which one is it, but I hoped if there is already wrapper for File::Find, it should have such search type somehow build in. Greetings TheMarty

Comment on File::Finder usage
Download Code
Re: File::Finder usage
by bpphillips (Friar) on Oct 29, 2004 at 12:46 UTC
    I can't tell by the code you've given above but I suspect when you're looking for a single file, you're calling it in scalar context which according to the docs will return the count of the files found rather than the list of files found.
    Here's an example of how you might use it:
    sub FindFile { my $file = shift; my @directories = @_; my $pathtofile; unshift(@directories,"./"); # this could still cause scalar context... # return File::Finder->name($file)->in(@directories); # this forces list context return @{[File::Finder->name($file)->in(@directories)]}; };
    Disclaimer: I've never used this module before 5 minutes ago

    Updated: returned the value from the File::Finder call directly rather than storing it in an array. I had first intended to only return the first element since the OP was only expecting one response and the name of the sub is FindFile not FindFiles but it's probably better to let the caller deal with multiple results rather than assume they only want one... (Thanks for the compliment merlyn :-)

    Updated (again): well tye pointed out the problem with my fix (as suggested by merlyn) so I've fixed it again...
      Disclaimer: I've never used this module before 5 minutes ago
      And yet, you nailed the proper usage and understanding of the problem on the first go! Good job! The only thing I would have done differently would have been to simply return the value from the execution, rather than store it into a new array and return that array, because the value will be the same.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

      Your function is susceptible to the same usage mistake that caused the original problem. This:

      my @files= File::Finder->name($­file)->in(@directori­es); return wantarray ? @files : "@files";

      or another use of wantarray might more appropriate. I'm using the Unix shell globbing scheme of error detection -- return something that isn't an existing file so that when you go to use it you should get an error message that contains useful information. There are more robust approaches and the best choice depends.

      - tye        

Re: File::Finder usage
by pg (Canon) on Oct 29, 2004 at 16:48 UTC

    And you don't need to say:

    my $file = shift(@_); my @directories = @_;

    Look at this example:

    foo(1,2,3,4,5,6); sub foo { my ($file, @directories) = @_; print $file, "\n"; print @directories; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://403691]
Approved by Corion
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-07-31 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls