Help - Search - Member List - Calendar
Full Version: Query on File::Find
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Shobha Deepthi
Hi,

Am using find method in File::Find module.
Right now this method is being used as follows:
find({wanted => &find_and_instantiate_commav_file,
no_chdir => 0},
$spec_entry);

But I want to pass another parameter to find_and_instantiate_commav_file
subroutine. I tried,
find({wanted => &find_and_instantiate_commav_file("some_value"),
no_chdir => 0},
$spec_entry);

And the sub routine is as follows:

sub find_and_instantiate_commav_file() {

my $slave_path = shift;

*LINENO:* if ( /,v$/ )
{
// do something
}
}

This fails with,
Use of uninitialized value in pattern match (m//) at ...*LINENO*
Not a CODE reference at /usr/local/oracle/perl/5.8.1/lib/File/Find.pm
line 792.

Can anyone let me know how to pass parameters to subroutine refernced in
find method and how to
access these within the subroutine.

--
Shobha Deepthi V
The statement below is true.
The statement above is false.

Jeff 'japhy' Pinyan
On Jul 7, Shobha Deepthi said:

QUOTE
But I want to pass another parameter to find_and_instantiate_commav_file
subroutine. I tried,
find({wanted => &find_and_instantiate_commav_file("some_value"),
no_chdir => 0},
$spec_entry);

The syntax

&function

returns a reference to a function. But the syntax

&function(...)

returns a reference to the RETURN value of function() called with whatever
args you've given it. What you want to do is:

find({
wanted => sub { find_and_instantiate_commav_file("some_value") },
no_chdir => 0,
}, $spec_entry);

Here, we create an anonymous function (sub { ... }) and use it. This
anonymous function, when called, just calls the find_...(...) function.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

John Doe
Shobha Deepthi am Donnerstag, 7. Juli 2005 06.19:
QUOTE
Hi,

Hi

probably not the best answer, but nobody else answered:

QUOTE
Am using find method in File::Find module.
Right now this method is being used as follows:
find({wanted => &find_and_instantiate_commav_file,
no_chdir => 0},
$spec_entry);

But I want to pass another parameter to find_and_instantiate_commav_file
subroutine. I tried,
find({wanted => &find_and_instantiate_commav_file("some_value"),

You can't pass an argument while defining a subroutine reference in general.

BTW, from the File::Find man page:

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

QUOTE
no_chdir => 0},
$spec_entry);

And the sub routine is as follows:

sub find_and_instantiate_commav_file() {

my $slave_path = shift;

No argument passed to the sub, so @_ is empty, and that's the reason for the
error message below.

QUOTE
*LINENO:*      if ( /,v$/ )
{
// do something
}
}

This fails with,
Use of uninitialized value in pattern match (m//) at ...*LINENO*
Not a CODE reference at /usr/local/oracle/perl/5.8.1/lib/File/Find.pm
line 792.

Can anyone let me know how to pass parameters to subroutine refernced in
find method and how to
access these within the subroutine.


You can use a lexical my-variable, which you set before invoking find, and
accessing from within the wanted sub.


hth, joe

Shobha Deepthi
Hi Jeff,

Thanks for your help. That really worked!

Shobha Deepthi V
The statement below is true.
The statement above is false.






Jeff 'japhy' Pinyan wrote:

QUOTE
On Jul 7, Shobha Deepthi said:

But I want to pass another parameter to
find_and_instantiate_commav_file subroutine. I tried,
find({wanted => &find_and_instantiate_commav_file("some_value"),
no_chdir => 0},
$spec_entry);


The syntax

&function

returns a reference to a function.  But the syntax

&function(...)

returns a reference to the RETURN value of function() called with
whatever args you've given it.  What you want to do is:

find({
wanted => sub { find_and_instantiate_commav_file("some_value") },
no_chdir => 0,
}, $spec_entry);

Here, we create an anonymous function (sub { ... }) and use it.  This
anonymous function, when called, just calls the find_...(...) function.



PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.