Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Taking reference to function

by targetsmart (Curate)
on Apr 16, 2009 at 04:01 UTC ( [id://757833]=note: print w/replies, xml ) Need Help??


in reply to Taking reference to function

Both have proper uses, generally it is good to use \&thefunc

but I will use 'funcname', when have a function template configured to be executed, where it can be easily changed, by adding a new function to the module and just adding a name to the configuration.
In which case in the main program, i read the configuration and just check whether that function exists using 'can', and execute it (if 'use strict' is in action, i will use 'no strict "refs"' inside that particular block so it won't throw error).

moreover the using 'funcname', does an additional lookup in the symbol table I reckon.


Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

Replies are listed 'Best First'.
Re^2: Taking reference to function
by GrandFather (Saint) on Apr 17, 2009 at 04:23 UTC

    can returns a code ref so you don't need to turn off strictures, just:

    use strict; use warnings; my $funcName = 'doit'; if (my $cando = main->can ($funcName)) { $cando-> (); } sub doit { print "I done it\n"; }

    Prints:

    I done it

    True laziness is hard work
Re^2: Taking reference to function
by SFLEX (Chaplain) on Apr 16, 2009 at 10:15 UTC
    Telling someone to turn off strict refs is NOT a good idea, unless right after the functions action they turn it back on.
    Besides why would you want to turn something off when you can get around the error.
    Spiel auf Hündinnen.
      If you turn off strict refs inside of a block, it is automatically on right after the block. I personally find it clearer to do this than to use a complex syntax the maintenance programmer may not know to fool strict.pm into not complaining.
      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://757833]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-07-18 02:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.