Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Adding functionality to an Object

by gellyfish (Monsignor)
on Jul 13, 2006 at 09:17 UTC ( #560896=note: print w/replies, xml ) Need Help??


in reply to Re^2: Adding functionality to an Object
in thread Adding functionality to an Object

In the first instance you might want to look at perlboot and perltoot as they both cover Inheritance.

As to adding methods to a class at runtime, yes that is entirely possible (you will find a number of modules that do this in a sub AUTOLOAD,) but you might want to carefully review your design as it is probably not the best solution in the general case.

However if you really insist on adding methods dynamically you can do something as simple as:

package Foo; + sub new { my ( $class ) = @_; return bless {}, $class; } + package main; + use strict; + my $foo = Foo->new(); + *Foo::zub = sub { print "I'm zub!\n"; }; + $foo->zub();

/J\

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2021-04-13 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?