我可以在没有先将对象保存到变量的情况下调用Perl OO函数吗?

Bry*_*eld 4 oop methods perl function handle

如何将这两个语句片段转换为单个语句?

my $handle = &get_handle('parameter');
$handle->do_stuff;
Run Code Online (Sandbox Code Playgroud)

有点像{&get_handle('parameter')}->do_stuff;,但正确的语法是什么?

ike*_*ami 11

不需要在左侧使用变量->.它可以是任何表达式,因此您可以简单地使用

get_handle('parameter')->do_stuff
Run Code Online (Sandbox Code Playgroud)

这实际上非常普遍.例如,

$self->log->warn("foo");          # "log" returns the Log object.
$self->response->redirect($url);  # "response" returns a Response object.
$self->config->{setting};         # "config"s return a hash.
Run Code Online (Sandbox Code Playgroud)