Perl: “Variable will not stay shared”

后端 未结 4 1599
无人共我
无人共我 2021-02-12 13:16

I looked up a few answers dealing with this warning, but neither did they help me, nor do I truly understand what Perl is doing here at all. Here\'s what I WANT it to do:

<
相关标签:
4条回答
  • 2021-02-12 13:57

    Following http://www.foo.be/docs/perl/cookbook/ch10_17.htm , you should define a local GLOB as follows :

    local *innerSub = sub {
        ...
    } 
    #You can call this sub without ->
    innerSub( ... ) 
    

    Note that even if warning is displayed, the result stay the same as it should be expected : variables that are not defined in the inner sub are modified in the outer sub scope. I cannot see what this warning is about.

    0 讨论(0)
  • 2021-02-12 14:11

    In brief, the second and later times outerSub is called will have a different $dom variable than the one used by innerSub. You can fix this by doing this:

    {
        my $dom;
        sub outerSub {
            $dom = ...
            ... innerSub() ...
        }
        sub innerSub {
            ...
        }
    }
    

    or by doing this:

    sub outerSub {
        my $dom = ...
        *innerSub = sub {
            ...
        };
        ... innerSub() ...
    }
    

    or this:

    sub outerSub {
        my $dom = ...
        my $innerSub = sub {
            ...
        };
        ... $innerSub->() ...
    }
    

    All the variables are originally preallocated, and innerSub and outerSub share the same $dom. When you leave a scope, perl goes through the lexical variables that were declared in the scope and reinitializes them. So at the point that the first call to outerSub is completed, it gets a new $dom. Because named subs are global things, though, innerSub isn't affected by this, and keeps referring to the old $dom. So if outerSub is called a second time, its $dom and innerSub's $dom are in fact separate variables.

    So either moving the declaration out of outerSub or using an anonymous sub (which gets freshly bound to the lexical environment at runtime) fixed the problem.

    0 讨论(0)
  • 2021-02-12 14:11

    If you want to minimize the amount of size passing parameters to subs, use Perl references. The drawback / feature is that the sub could change the referenced param contents.

    my $dom = someBigDOM;
    my $resultVar = doStuffWith(\$dom);
    
    
    sub doStuffWith {
       my $dom_reference = shift;
       my $dom_contents = $$dom_reference;
       #...
    }
    
    0 讨论(0)
  • 2021-02-12 14:16

    You need to have an anonymous subroutine to capture variables:

    my $innerSub = sub  {
      my $resultVar = doStuffWith($dom);
      return $resultVar;
    };
    

    Example:

    sub test {
        my $s = shift;
    
        my $f = sub {
            return $s x 2;
        };  
    
        print $f->(), "\n";
    
        $s = "543";
    
        print $f->(), "\n";
    }
    
    test("a1b");
    

    Gives:

    a1ba1b
    543543
    
    0 讨论(0)
提交回复
热议问题