Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > scoping question

Reply
Thread Tools

scoping question

 
 
Graham Wood
Guest
Posts: n/a
 
      08-05-2005
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:rint_both() or is there a shorter way to do it?

Thanks

Graham

 
Reply With Quote
 
 
 
 
Graham Wood
Guest
Posts: n/a
 
      08-05-2005
There's a syntax error in the code but I'm hoping the question still
makes sense.

Graham

Graham Wood wrote:

> 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:rint_both() or is there a shorter way to do it?
>
> Thanks
>
> Graham
>


 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      08-05-2005
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:rint_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:rint_both:

*packagename:rint_both = \&main:rint_both;

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

Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
Graham Wood
Guest
Posts: n/a
 
      08-05-2005
Tassilo v. Parseval wrote:

> 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.

<snip>

> $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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python newbie - question about lexical scoping Matt Barnicle Python 10 12-02-2007 08:59 PM
scoping question 2b|!2b==? C++ 1 04-21-2007 03:05 AM
A scoping question It's me Python 8 12-28-2004 11:33 PM
Beginner's scoping question Alan Little Python 6 11-12-2004 07:44 PM
JSP, JavaBeans and Forms scoping question. Strange behaviour? Matt S. Java 0 05-04-2004 11:58 AM



Advertisments