Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Calling subroutine with a scalar

by Rodster001 (Pilgrim)
on May 06, 2015 at 18:07 UTC ( [id://1125879]=perlquestion: print w/replies, xml ) Need Help??

Rodster001 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I know this has been asked endlessly, but I am doing it slightly different than I have done before and I cannot quite get the syntax right.
my $res = new Some::Package({ foo => bar })->subroutine;
What I want to do is call "subroutine" by variable. For example:
my $sub = "subroutine"; my $res = new Some::Package({ foo => bar })->( $sub );
That doesn't work, I've tried several variations of that (along with \&) and I can't quite get it. Can someone enlighten me?

Thanks!

Replies are listed 'Best First'.
Re: Calling subroutine with a scalar
by philipbailey (Curate) on May 06, 2015 at 18:38 UTC

    Try

    my $res = new Some::Package({ foo => bar })->$sub;

    Or better:

    my $res = Some::Package->new({ foo => bar })->$sub;

      That did it. Thank you. Why is it better to call new that way?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1125879]
Approved by philipbailey
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found