Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Is modifying the symbol table to redefine subroutines evil?

by Limbic~Region (Chancellor)
on Apr 11, 2007 at 21:59 UTC ( #609502=note: print w/ replies, xml ) Need Help??


in reply to Is modifying the symbol table to redefine subroutines evil?

tlm,
Depends on your definition of evil and your perspective. Messing with the symbol table like this can certainly be useful. I think a more common version of call_me_only_once() for this would be:

sub foo { no warnings 'redefine'; if ($cond1) { *foo = sub { ... }; } elsif ($cond2) { *foo = sub { ... }; } else { *foo = sub { ... }; } goto &foo; }
The objective being to determine which version of foo() to use (perhaps XS, module, roll-your-own) only once so that subsequent calls are faster.

Cheers - L~R


Comment on Re: Is modifying the symbol table to redefine subroutines evil?
Download Code
Re^2: Is modifying the symbol table to redefine subroutines evil?
by tlm (Prior) on Apr 11, 2007 at 22:03 UTC

    call_me_only_once() was meant to represent code that must be executed only on the first call to the function. I hadn't thought of the scenario you present, but I can see that the self-redefinition technique could be useful there too.

    the lowliest monk

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-12-26 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls