6

我想在另一个子里面有一个子,

sub a {
    sub b {
    }
}

我想sub b为每次调用创建一个新实例sub a。有没有办法在 Perl 中做到这一点?

当我运行上面的代码并打印 in 的地址时sub bsub a我总是得到相同的sub b地址

sub a {
    print \&b;
    sub b{
    }
}

Perl Monks 上的这个链接说我们可以这样做,但我总是看到相同的地址sub b

有没有办法sub b为每次调用创建一个新实例sub a

4

4 回答 4

12
sub a {
    sub b{
    }
}

基本上是一样的:

sub a {

}
sub b{
}

因为命名子程序存在于符号表中,因此它们是全局的。您将需要返回对子例程的引用。

于 2012-10-13T14:46:58.720 回答
8

命名子程序只创建一次。您需要返回一个匿名子例程引用,如下所示:

sub a {
    my $counter = 1;
    return sub {
        return $counter++;
    }
}

my $c1 = a();
my $c2 = a();

# different references
print "c1 = $c1, c2 = $c2\n";

# each has a different counter
print "c1 ", $c1->(), "\n";
print "c1 ", $c1->(), "\n";
print "c2 ", $c2->(), "\n";
print "c2 ", $c2->(), "\n";
于 2012-10-13T14:36:29.193 回答
7

您可以创建对匿名子的引用:

#!/usr/bin/env perl
use strict;
use warnings;

sub a
{
    my($b) = @_;
    my $subref = sub { my($a) = @_; print "a = $a; b = $b\n"; return $a + $b; };
    &$subref(3);
    return $subref;
}

my $sub1 = a(10);
my $a10  = &$sub1(19);
my $sub2 = a(20);
my $a20  = &$sub2(20);
print "a10 = $a10; a20 = $a20; sub1 = $sub1; sub2 = $sub2\n";

样本输出:

a = 3; b = 10
a = 19; b = 10
a = 3; b = 20
a = 20; b = 20
a10 = 29; a20 = 40; sub1 = CODE(0x7ffc3c002eb8); sub2 = CODE(0x7ffc3c032eb8)
于 2012-10-13T15:15:29.390 回答
3
sub a {
    my $b = sub {
    };
    print \&$b;
}

或全局:

sub a {
    local *b = sub {
    };
    print \&b;
}
于 2012-10-13T17:02:31.460 回答