我如何在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);
最佳答案
要在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的执行由于编译错误而中止。