Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 313
700 Software Avatar asked Nov 30 '22 14:11

700 Software


2 Answers

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.
like image 133
ikegami Avatar answered Dec 05 '22 15:12

ikegami


get_handle('parameter')->do_stuff

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

like image 23
daxim Avatar answered Dec 05 '22 15:12

daxim