如何将数组放入POE堆中并推送或弹出数据?

3

我该如何将一个数组放置在POE堆栈上,并且向其中推入/弹出数据?

我想要将下面的数组放置在堆栈上:

@commands = (
    ["quit",\&Harlie::Commands::do_quit,10],
    ["part",\&Harlie::Commands::do_part,10],
    ["join",\&Harlie::Commands::do_join,10],
    ["nick",\&Harlie::Commands::do_nick,10],
    ["module",\&Harlie::Commands::do_modules,10],
    ["uptime",\&Harlie::Commands::do_uptime,0]
);

我如何访问其中包含的函数引用?目前,我可以通过以下方式运行它们:

@commands->[$foo]->(@bar);

我猜想你是在问这个问题的答案是否很简单,只需要回答“是”或“否”即可。
$heap->{commands}->[$foo]->(@bar);
1个回答

0

要在POE堆上创建/使用数组,只需将引用包装在"@{...}"中即可。 例如:

use strict;
use warnings;
use POE;
use POE::Kernel;

POE::Session->create(
    inline_states =>{
        _start =>   \&foo,
        bar    => \&bar}  
);

sub foo{
    my ($kernel, $heap) = @_[KERNEL, HEAP];
    @{$heap->{fred}} = ("foo","bar","baz");
    $kernel->yield("bar");
}

sub bar{
    my ($kernel, $heap) = @_[KERNEL, HEAP];
    print "Length of array fred... " . ($#{$heap->{fred}}+1) . "\n";
    print "Contents of fred... ";
    foreach(@{$heap->{fred}}){
    print $_ . " ";  }
    print "\n";
}

POE::Kernel->run();

然而,数组的数组并不是那么简单。从上面逻辑上接下来的程序...

use strict;
use warnings;
use POE;
use POE::Kernel;

POE::Session->create(
    inline_states    => {
    _start =>    \&foo,
    bar    =>    \&bar
    }
    );

sub foo{
    my ($kernel, $heap) = @_[KERNEL, HEAP];

    @{$heap->{fred}} = (
        ["foo","bar","baz"],
        ["bob","george","dan"]
    );
    $kernel->yield("bar");
}

sub bar{
    my ($kernel, $heap) = @_[KERNEL, HEAP];
    print "Length of array fred... " . ($#{$heap->{fred}}+1) . "\n";
    print @{$heap->{fred}}[0][0];

}

POE::Kernel->run();

...仅仅给出以下错误信息。

perl ../poe-test.pl

在../poe-test.pl的第26行附近有语法错误,如“][”

由于编译错误,../poe-test.pl的执行被中止。


应该写成print ${$heap->{fred}}[0][0];或者print $heap->{fred}->[0][0]; - Joshua

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接