Perl: “Variable will not stay shared”

后端 未结 4 1603
无人共我
无人共我 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 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
    

提交回复
热议问题