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?