问题描述:

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.
相关阅读:
Top