scoping question

Discussion in 'Perl Misc' started by Graham Wood, Aug 5, 2005.

  1. Graham Wood

    Graham Wood Guest

    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
     
    Graham Wood, Aug 5, 2005
    #1
    1. Advertising

  2. Graham Wood

    Graham Wood Guest

    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::print_both() or is there a shorter way to do it?
    >
    > Thanks
    >
    > Graham
    >
     
    Graham Wood, Aug 5, 2005
    #2
    1. Advertising

  3. 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
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Aug 5, 2005
    #3
  4. Graham Wood

    Graham Wood Guest

    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
     
    Graham Wood, Aug 5, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt S.
    Replies:
    0
    Views:
    423
    Matt S.
    May 4, 2004
  2. Alan Little

    Beginner's scoping question

    Alan Little, Nov 10, 2004, in forum: Python
    Replies:
    6
    Views:
    274
    Alan Little
    Nov 12, 2004
  3. It's me

    A scoping question

    It's me, Dec 28, 2004, in forum: Python
    Replies:
    8
    Views:
    346
    Dennis Lee Bieber
    Dec 28, 2004
  4. 2b|!2b==?

    scoping question

    2b|!2b==?, Apr 21, 2007, in forum: C++
    Replies:
    1
    Views:
    300
    Ivan Vecerina
    Apr 21, 2007
  5. Matt Barnicle
    Replies:
    10
    Views:
    663
    Bruno Desthuilliers
    Dec 2, 2007
Loading...

Share This Page