如何在Perl子例程中使用$ a和$ b

问题描述 投票:10回答:2

我想在我的匿名二进制函数中使用$a$b变量,就像它在sort {$a <=> $b} (1, 2, 3)中完成但我无法弄清楚为什么代码像

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

Foo::Bar(sub { $a + $b });

package Foo;
sub Bar {
    my ($function) = @_; 

    for my $i (1, 2, 3) {
        local ($a, $b) = ($i, $i);
        print $function->() . "\n";
    }
}    

不起作用。而

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

Foo::Bar(sub { $_ });

package Foo;
sub Bar {
    my ($function) = @_; 

    for my $i (1, 2, 3) {
        local $_ = $i;
        print $function->() . "\n";
    }
}

工作良好。

我究竟做错了什么?

perl scope global-variables special-variables
2个回答
14
投票

$a$b是特殊的包变量。你在Foo::Bar包中调用main,所以你需要设置$main::a$main::b才能使它工作。您可以使用caller获取调用包的名称。这应该工作:

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

Foo::Bar(sub { $a + $b });

package Foo;
sub Bar {
    my ($function) = @_; 
    my $pkg = caller;

    for my $i (1, 2, 3) {
        no strict 'refs';
        local *{ $pkg . '::a' } = \$i;
        local *{ $pkg . '::b' } = \$i;
        print $function->() . "\n";
    }
}    

0
投票

如果有人有兴趣,可以从List::MoreUtils::PP v.0.428(截至2017年12月)复制粘贴:

# begin copyrighted content
sub reduce_u(&@)
{
    my $code = shift;

    # Localise $a, $b
    my ($caller_a, $caller_b) = do
    {
        my $pkg = caller();
        no strict 'refs';
        \*{$pkg . '::a'}, \*{$pkg . '::b'};
    };

    local (*$caller_a, *$caller_b);
    *$caller_a = \();
    for (0 .. $#_)
    {
        *$caller_b = \$_[$_];
        *$caller_a = \($code->());
    }

    ${*$caller_a};
}
# end copyrighted content

它仅与受上述no strict 'refs';影响的区域中的上述样本不同。如果没有严格要求,可能无法完成。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.