http://www.perlmonks.org?node_id=1015910

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

Hi is it possible to add methods and properties at runtime,like in Python? and why would there be need to do that,why is that an advantage anyway?

Replies are listed 'Best First'.
Re: add method at runtime
by LanX (Saint) on Jan 29, 2013 at 17:23 UTC
    > Hi is it possible to add methods and properties at runtime,like in Python?

    Yes, semantically pretty much the same way.

    For instance see Re: change perl function in runtime

    > and why would there be need to do that,why is that an advantage anyway?

    Monkey patching is a way for module users to dynamically adjust foreign modules to their temporary needs w/o changing the installed and officially maintained code.

    UPDATE

    Ah roboticus did good in mentioning Autoloading. It's a technique for module authors (not module users) who want to decide to compile functions only on demand.

    (For instance this can improve compile/load time. IIRC does (did?) CGI.pm something comparable.)

    Cheers Rolf

Re: add method at runtime
by roboticus (Chancellor) on Jan 29, 2013 at 17:23 UTC

    Yes, it's possible. The reasons and benefits, however, vary with the type of task you're doing.

    Generally, when you find the need to do it, you'll pretty much know the advantages. At that point, you probably need to contemplate the disadvantages as well, though. ;^)

    Update: Check out How can I use AUTOLOAD to magically define get and set methods for my member data? for an example.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.