in reply to Is it legal to create subs in a module?
It's far better instead to compile that code once, plugging in the parts that are variable, using a closure, like:eval "sub $action { 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.{ my $action = "whatever"; no strict 'refs'; *{$action} = sub { my $pkg = shift; return $pkg->{$action}; }; }
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
|
|---|