How does one get a method reference when using Moose

喜夏-厌秋 提交于 2019-12-06 09:38:51
Eric Strom

The question I linked to above goes into detail about the various options when encapsulating a method call in a code ref. In your case, I would write the main package as:

my $storage = Storage->new;

my $parser = Parser->new;
$parser->generic_batch_store(sub {$storage->batch_store(@_)});

$parser->parse;

$storage is changed to a lexical so that the code reference sub {$storage->batch_store(@_)} can close over it. The (@_) added to the end allows arguments to be passed to the method.

I am not a Moose expert, but I believe that you will need to call the code with an additional dereferencing arrow:

$self->generic_batch_store->(\@buf);

which is just shorthand for:

($self->generic_batch_store())->(\@buf);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!