Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Adding functionality to an Object

by gellyfish (Monsignor)
on Jul 13, 2006 at 09:17 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://560896]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found