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

Quick File::Find Question

by r.joseph (Hermit)
on May 06, 2001 at 04:49 UTC ( [id://78308]=perlquestion: print w/replies, xml ) Need Help??

r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

Hey All! I know that when you call File::Find::find(\&wanted,"/foo/bar");, the find() function passes some stuff to the wanted() function that it uses internally.

My question is, how do I pass data to the wanted() function without having it complain? Here is a small snippet of code I tried that attemps to pass two filehandles to the wanted() function:
while (<MAINDB>) { find(\&wanted(\*C_LOG, \*R_LOG), $_); }
MAINDB is just a DB of directories to search, and C_LOG and R_LOG are simply two log files that program data needs to print to. However, when I try this, I get this message:
Not a CODE reference at C:/Perl/lib/File/Find.pm line 432, <MAINDB> line 1.
I am assuming that this is dealing with the \&wanted function call and also assume that it is complaining because I tried to pass parameters to it - any thoughts on why this is happening?

P.S. Here is my wanted() function:
sub wanted { my $clog_filehandle = shift; my $rlog_filehandle = shift; # $_ is file name (File::Find thing) my $file = $_; print "In wanted(), checking $file...\n"; copy_file( $file, $clog_filehandle, $rlog_filehandle) if (-f $f); }
Pretty simple - just checks if the file is actually a file (not a directory or something else) and if so calls copy_file([filename],[filehandle1],[filehandle2]); Guess that wasn't exactly a short question, but it should be quick work for my fellow monks who are much more intelligent than I! Thanks a ton!

r. j o s e p h
"Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

Replies are listed 'Best First'.
Re: Quick File::Find Question
by buckaduck (Chaplain) on May 06, 2001 at 05:57 UTC
    Call your subroutine indirectly with an anonymous code reference instead:
    while (<MAINDB>) { find( sub { wanted(\*C_LOG, \*R_LOG, $_) }, $_); }
    If you like you can use my $file = shift; in the subroutine, instead of getting it from $_. It shouldn't actually matter, but I'm starting to see too many $_ variables in this short piece of code...

    buckaduck

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://78308]
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: (3)
As of 2024-03-28 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found