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

What is the equivalent of

$class->$method( $args );

for non-OO packages? I'd like to do

$module::$function( $args );

but that results in a syntax error. I could use

$module->$function( undef, $args );

but I'm sure there is a better way to do it. Can someone guide me to the higher Perl path?

Replies are listed 'Best First'.
Re: Calling $function from $module
by ikegami (Patriarch) on Aug 05, 2015 at 22:41 UTC
    The equivalent of
    $invocant->$method_name( @args )
    is
    ($module.'::'.$function_name)->( @args ) # Perl 5.6+
    or
    &{ $module.'::'.$function_name }( @args )

    use strict qw( refs ); forbids these (as it should), so you can use the following:

    my $ref = do { no strict qw( refs ); \&{ $module.'::'.$function_name } }; $ref->( @args )

    Except it turns out that \&{...} is exempt from strict reference enforcement, so you can simply use

    my $ref = \&{ $module.'::'.$function_name }; $ref->( @args )

    or even

    ( \&{ $module.'::'.$function_name } )->( @args )

    This trick to bypass strict reference enforcement is not documented.

      "These tricks to by pass strict reference enforcement are not documented."

      Nor should they be, imho.

        These aren't tricks and they are documented

        Also Symbol::qualify_to_ref

      Thank you so much -- this is great!
Re: Calling $function from $module
by BrowserUk (Patriarch) on Aug 05, 2015 at 22:19 UTC

    Does this help?

    package fred; sub fred{ print "This is fred::fred; so good they named me twice!" };; package main; fred::fred;; This is fred::fred; so good they named me twice! $p= 'fred'; $s='fred';; &{ "$p\:\:$s" };; This is fred::fred; so good they named me twice! *{ "$p\:\:$s" }->();; This is fred::fred; so good they named me twice!

    Anyone got any experience of this phone's predecessor?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
      'fred::fred'->();;
Re: Calling $function from $module (can)
by tye (Sage) on Aug 06, 2015 at 03:29 UTC
    $module->can($function)->( @args );

    - tye        

Re: Calling $function from $module
by stevieb (Canon) on Aug 05, 2015 at 22:32 UTC
    Could you provide some context in which you need to use this type of syntax?

      I'm refactoring a big if/else decision tree that sets some $args, loads a module, possibly does other random stuff, and then calls a function from that module. Previously each elsif had contained code to set $args, load the module, do the random stuff, and call the function. Being able to call  ( &{ $module.'::'.$function } )->( $args ); gets rid of much of the repetitive code.