3

How do I put an array on the POE heap, and push/pop data to/from it?

I'm trying to put the following array on the heap:

@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]
);

And how would I be able to access the function references contained within? Currently, I can run them via:

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

Would I be correct in assuming it would simply be?:

$heap->{commands}->[$foo]->(@bar);
4

1 回答 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 的执行由于编译错误而中止。`

于 2010-02-01T13:03:35.197 回答