http://qs321.pair.com?node_id=560896


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\