Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

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


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

I fail to see how re-arranging a single type glob entry is more convoluted (or complex) than having a state variable sitting around which is changed only once and tested every time the sub is called.

Seriously? Using the variable is very simple, and the intent should be pretty obvious to anyone if the variable has a clear name. To me, it seems a lot less complex than a self-modifying program. The typeglob approach should at least have a comment explaining its purpose.


Comment on Re^5: Is modifying the symbol table to redefine subroutines evil?
Re^6: Is modifying the symbol table to redefine subroutines evil?
by shmem (Canon) on Apr 12, 2007 at 21:09 UTC
    To me, it seems a lot less complex than a self-modifying program.

    Oh wait... self-modifying? In what sense? All the code there is, is there in the first place. As with the OP's code, we are talking of a named sub, whose code reference is replaced by an anonymous subroutine at the first call of that named sub. The code block for that anonymous subroutine reference is inside the named sub, and it is called at the first call of that named sub, and at all calls of that sub thereafter.

    No code is generated, and no code is modified at runtime. There's a conditional that is optimized away, not by the perl interpreter but explicitly by the programmer.

    If we would label that coding as writing a self-modifying program, then using e.g. POSIX would also comprise writing a self-modifying program - and all programs that use the AutoLoader, and all programs that pull in any module via use, require or do file.

    --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}
      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.

        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://609730]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2014-07-10 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (212 votes), past polls