gpt4 book ai didi

arrays - 如何将数组放在 POE 堆上并推送或弹出数据?

转载 作者:行者123 更新时间:2023-12-02 05:14:26 24 4
gpt4 key购买 nike

如何在 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

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

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

关于arrays - 如何将数组放在 POE 堆上并推送或弹出数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2025345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com