in reply to File::Finder usage

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...

Replies are listed 'Best First'.
•Re^2: File::Finder usage
by merlyn (Sage) on Oct 29, 2004 at 13:27 UTC
    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.

Re^2: File::Finder usage
by tye (Sage) on Oct 29, 2004 at 16:21 UTC

    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