Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^7: Is this evil?

by perrin (Chancellor)
on Apr 12, 2007 at 21:29 UTC ( #609757=note: print w/ replies, xml ) Need Help??


in reply to Re^6: Is modifying the symbol table to redefine subroutines evil?
in thread Is modifying the symbol table to redefine subroutines evil?

The subroutine changes its own definition. I'd certainly call that self-modifying. The exporter and friends just alias things into other namespaces.

I do use code that does things like this, mostly for accessor methods. Those don't have an obvious simpler alternative.


Comment on Re^7: Is this evil?
Re^8: Is modifying the symbol table to redefine subroutines evil?
by shmem (Canon) on Apr 12, 2007 at 21:35 UTC
    The subroutine changes its own definition. I'd certainly call that self-modifying.

    No, it doesn't. The subroutine's definition is what is inside the code block which gets assigned to the CODE typeglob entry of that named sub. The code reference is changed, nothing else. The rest is providing for something like an INIT block attached to a subroutine.

    That's not clear, at first glance, wich is the only objection I have to it. I wonder whether Perl 6 has some better semantics for that.

    update: using the AutoLoader, the OP's code could be rewritten in a foo.al:

    call_me_only_once(); sub foo { # the real foo() ... } 1;

    and we would all be happy autoloading foo and having call_me_only_once() be called just once.

    Would that be self-modifying code?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (16)
As of 2014-11-26 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (171 votes), past polls