scoping question

G

Graham Wood

I'm calling find (from File::Find) in a loop inside another subroutine
and I want to access the value of $scope in the find subroutine.

====================================================================================
sub split_files{
print_both("splitting files\n");
foreach $scope (@scopes){
print_both("\tin $scope\n");
chdir("$here/$scope"){
find(\&wanted,'.');
}
}
print_both("finished\n");
}

sub wanted{
push(@{$files{$scope}}, $File::Find::name);
}
====================================================================================

If I use

foreach my $scope (@scopes){
}

The value of scope is not accessible in wanted(). How should I scope
$scope so that wanted() can see it?

If I use a package in split_files() and $packagename::scope to get the
value in wanted(), do I then have to change every reference to
print_both() to $main::print_both() or is there a shorter way to do it?

Thanks

Graham
 
G

Graham Wood

There's a syntax error in the code but I'm hoping the question still
makes sense.

Graham
 
T

Tassilo v. Parseval

Also sprach Graham Wood:
I'm calling find (from File::Find) in a loop inside another subroutine
and I want to access the value of $scope in the find subroutine.

====================================================================================
sub split_files{
print_both("splitting files\n");
foreach $scope (@scopes){
print_both("\tin $scope\n");
chdir("$here/$scope"){
find(\&wanted,'.');
}
}
print_both("finished\n");
}

sub wanted{
push(@{$files{$scope}}, $File::Find::name);
}
====================================================================================

If I use

foreach my $scope (@scopes){
}

The value of scope is not accessible in wanted(). How should I scope
$scope so that wanted() can see it?

$scope can easily remain a lexical if you fix the call to find():

foreach my $scope (@scopes) {
...
find(sub { wanted($scope) }, '.');
}

Additionally, wanted() now receives one parameter:

sub wanted {
my $scope = shift;
push @{ $files{$scope} }, $File::Find::name;
}
If I use a package in split_files() and $packagename::scope to get the
value in wanted(), do I then have to change every reference to
print_both() to $main::print_both() or is there a shorter way to do it?

You can put print_both() into that other package and have it export this
function via the Exporter mechanism. Or more easily, alias
main::print_both:

*packagename::print_both = \&main::print_both;

Now print_both() lives both in 'main' and in 'packagename'.

Tassilo
 
G

Graham Wood

Tassilo said:
Also sprach Graham Wood:
$scope can easily remain a lexical if you fix the call to find():

foreach my $scope (@scopes) {
...
find(sub { wanted($scope) }, '.');
}

Additionally, wanted() now receives one parameter:

sub wanted {
my $scope = shift;
push @{ $files{$scope} }, $File::Find::name;
}

Thanks Tassilo, working now.

Graham
 

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
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top