开发者

How do I put an array on the POE heap and push or pop data?

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:开发者_C百科: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);


To create/use an array on the POE heap, it's merely a case of wrapping the reference in "@{...}". e.g.:

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();

However, arrays of arrays are not that simple. The program that logically follows on from the above...

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();

...merely gives the following error.

perl ../poe-test.pl

syntax error at ../poe-test.pl line 26, near "]["

Execution of ../poe-test.pl aborted due to compilation errors.`

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜