Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Is it legal to create subs in a module?

by merlyn (Sage)
on Oct 11, 2001 at 19:27 UTC ( [id://118233]=note: print w/replies, xml ) Need Help??


in reply to Is it legal to create subs in a module?

First, please don't use string eval for something like:
eval "sub $action { my \$pkg = shift; return(\$pkg->{\"$action\"}); }" +;
It's far better instead to compile that code once, plugging in the parts that are variable, using a closure, like:
{ my $action = "whatever"; no strict 'refs'; *{$action} = sub { my $pkg = shift; return $pkg->{$action}; }; }
There. No nasty runtime eval, meaning no "firing up the compiler" every time you add a method (s-l-o-o-o-o-o-w), nor worries that if $action contains quote-character messiness, you'll get clobbered.

Second, the class package (where the methods are kept) is not per-object, but shared amongst all objects. So if one object added a "blah" method, that would be seen by all objects.

Maybe what you want is one of the classless object solutions, like Class::Prototyped by the Monestary's own bikeNomad. That lets you create lightweight classes with methods that are specific to that object. In fact, you can then inherit from that (very Self-like, if you're familiar with Self).

-- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-18 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found