如何将数组放入 POE 堆并推送或弹出数据?

发布于 2024-08-17 03:58:07 字数 663 浏览 6 评论 0原文

如何将数组放在 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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-08-24 03:58:07

要在 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 的执行被中止。`

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.`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文