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:
umper;
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]);
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:
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]);