How to put an array in a bunch of POEs and click or put data?

How to put an array in a POE heap and push / put data to / from it?

I am trying to put the following array in a 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 can I access the function links contained inside? Currently, I can run them through:

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

Did I guess it would be easy ?:

$heap->{commands}->[$foo]->(@bar);
+3
source share
1 answer

To create / use an array on the POE heap is just a case of wrapping the link in "@ {...}". eg:.

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. A program that logically follows 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();

... just gives the following error.

perl../poe-test.pl

.. /poe -test.pl 26, "] ["

../poe-test.pl - .

0

Source: https://habr.com/ru/post/1727724/


All Articles