当前位置: 动力学知识库 > 问答 > 编程问答 >

oop - Can I call a Perl OO function without first saving the object to a variable?

问题描述:

How can I turn this two statement snippet into a single statement?

my $handle = &get_handle('parameter');

$handle->do_stuff;

Something like {&get_handle('parameter')}->do_stuff;, but what would be the correct syntax?

网友答案:
get_handle('parameter')->do_stuff

Related: When should I use the & to call a Perl subroutine?

网友答案:

There's no requirement for a variable to be used on the left-hand side of the ->. It can be any expression, so you can simply use

get_handle('parameter')->do_stuff

It's actually quite common. For example,

$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.
分享给朋友:
您可能感兴趣的文章:
随机阅读: