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

Using File::Find from a function.

by balajinagaraju (Sexton)
on Mar 19, 2013 at 05:38 UTC ( [id://1024209]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, My requirement is to expose a perl function from our product to the users(other developers using our product) to find whether a file is present in a particular directory. For this the user calls my function with the Directory name and Filename as parameters. Internally in my function definition i need to handle the file finding part and i am using File::find for this purpose, just a snippet is below

use File::Find; my $dir = "C:\\Users\\Public\\ABCD\\Core\\sdk\\PAT"; find(\&myfile, $dir); sub myfile{ my $file = C:\\Users\\Public\\ABCD\\Core\\sdk\\PAT\pat.xml"; if(-e $file){ print "Exists"; } else{ print "Doesn't exits"; } }

In this example currently i am hardcoding the filename inside my myfile function but ideally it would be a parameter recieved from a function call, here 'Find' takes the function reference and the directory name as the parameter now how do i send the filename along with the function reference and directory name to the 'Find' method.

In short 1) The user calls myfunction(Directoryname , Filename) 2)Myfunction definition should invoke the 'Find' method with the default parameters as well as the Filename mentioned in the users call. How do i accomplish this?. Hopefully the problem is clear.

Replies are listed 'Best First'.
Re: Using File::Find from a function.
by vinoth.ree (Monsignor) on Mar 19, 2013 at 06:23 UTC

    From CPAN doc

    find

    find(\&wanted, @directories); find(\%options, @directories);

    find() does a depth-first search over the given @directories in the order they are given. For each file or directory found, it calls the &wanted subroutine.Additionally, for each directory found, it will chdir() into that directory and continue the search, invoking the &wanted function on each file or subdirectory in the directory.

    Update:

    You can do this way.

    my $file='foo'; my directory='a'; + sub check_existance { + if ( -e $_ && $_ eq $file ) { + print "Found file '$_' in directory '$File::Find::dir'\n"; + } + } find( \&check_existance, $directory );

    The check_existance function takes no arguments but rather does its work through a collection of variables.

    $File::Find::dir is the current directory name,

    $_ is the current filename within that directory

    $File::Find::name is the complete pathname to the file.

    The above variables have all been localized and may be changed without affecting data outside of the check_existance function.

    Beginners guide to File::Find

    All is well
Re: Using File::Find from a function.
by Anonymous Monk on Mar 19, 2013 at 06:48 UTC

    My requirement is to expose a perl function from our product to the users(other developers using our product) to find whether a file is present in a particular directory.

    That can be accomplished using -e operator (perldoc -f -e), it doesn't sound like a use case for File::Find at all

      Hi, Thanks for your suggestions , using -e works fine.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2025-07-12 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.