如何在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);
答案 0 :(得分:0)
要在POE堆上创建/使用数组,它只是将引用包装在“@ {...}”中的情况。 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();
然而,阵列数组不那么简单。从上面逻辑上遵循的程序...
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的执行中止。