Passing paramaters to the File::Find find() function

N

niall.macpherson

I've just used the File::Find module for the first time.

I can't figure out how to pass variables into the find() function.

Here is a trivial example (I know that what this does can be done many
other better ways, but it was about the smallest bit of code that I
could come up with which illustrates my problem)

The point is that $mytext and @myfiles both have to be global variables
since there doesn't seem to be a way of passing them from the
testclpm7() sub to the pushfiles() sub via the find() call.

Is it possible to pass values through so I can avoid having to use
global variables ?

Thanks

use strict;
use warnings;
use File::Find;
use Data::Dumper;
use vars qw (@myfiles);
use vars qw ($mytext);

#-----------------------------------------------------------
sub pushfiles
{
if(!/$mytext/)
{
push @myfiles, $File::Find::name;
}
return;
}
#-----------------------------------------------------------
sub testclpm7
{
($mytext) = @_;
## Works but I would like to pass both $mytext and @myfiles
## here so that they don't have to be global
find (\&pushfiles , "C:/develop/NiallPerlScripts");
print Dumper \@myfiles;
}
#-----------------------------------------------------------
if($#ARGV != 0)
{
die "Usage $0 <text>";
}
testclpm7($ARGV[0]);
 
A

Anno Siegel

I've just used the File::Find module for the first time.

I can't figure out how to pass variables into the find() function.

Here is a trivial example (I know that what this does can be done many
other better ways, but it was about the smallest bit of code that I
could come up with which illustrates my problem)

The point is that $mytext and @myfiles both have to be global variables
since there doesn't seem to be a way of passing them from the
testclpm7() sub to the pushfiles() sub via the find() call.

Is it possible to pass values through so I can avoid having to use
global variables ?
[snip]

sub testclpm7
{
($mytext) = @_;
## Works but I would like to pass both $mytext and @myfiles
## here so that they don't have to be global
find (\&pushfiles , "C:/develop/NiallPerlScripts");

Since File::Find::find doesn't take these parameters you won't have
much joy giving them to it. You need to write your own variant.
print Dumper \@myfiles;
}


Untested:

sub my_find {
my ( $myfiles, $mytext, $dir) = @_;
File::Find::find(
sub { push @$myfiles, $File::Find::name if /$mytext/ },
$dir,
);
}

Anno
 
A

Anno Siegel

I've just used the File::Find module for the first time.

I can't figure out how to pass variables into the find() function.

Here is a trivial example (I know that what this does can be done many
other better ways, but it was about the smallest bit of code that I
could come up with which illustrates my problem)

The point is that $mytext and @myfiles both have to be global variables
since there doesn't seem to be a way of passing them from the
testclpm7() sub to the pushfiles() sub via the find() call.

Is it possible to pass values through so I can avoid having to use
global variables ?
[snip]

sub testclpm7
{
($mytext) = @_;
## Works but I would like to pass both $mytext and @myfiles
## here so that they don't have to be global
find (\&pushfiles , "C:/develop/NiallPerlScripts");

Since File::Find::find doesn't take these parameters you won't have
much joy giving them to it. You need to write your own variant.
print Dumper \@myfiles;
}


Untested:

sub my_find {
my ( $myfiles, $mytext, $dir) = @_;
File::Find::find(
sub { push @$myfiles, $File::Find::name if /$mytext/ },
$dir,
);
}

Call as

my @myfiles;
my_find( \ @myfiles, '\.jpg$', 'C:/develop/NiallPerlScripts');


Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top