Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

add method at runtime

by Anonymous Monk
on Jan 29, 2013 at 17:17 UTC ( #1015910=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on add method at runtime
Re: add method at runtime
by LanX (Canon) 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 (Canon) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015910]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

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

    How do you remember the number of days in each month?











    Results (58 votes), past polls