On 01/13/2012 07:25 PM, Parag Kalra wrote:
my $obj = FooBar->new;
$obj->{'new_key'} = 'some_value'
Now I am not sure if that is the correct way of inserting a new data
structure into an already bless reference
1. FooBar may or may not be implemented as a hashref.
2. It's not wise to muck with the internals of an object (violates
encapsulation).
Aggregation seems to be the cleanest approach, as suggested by Shawn H
Corey:
http://www.mail-archive.com/beginners%40perl.org/msg112878.html
But, you will need to figure out how to pass through method calls to the
inner object (via AUTOLOAD?) and deal with the possibility of your
accessor name colliding with a future contained class method name (via
UNIVERSAL::can?).
That said, if you know that FooBar is implemented as a hashref, that
FooBar (and everything else) does not use FooBar's 'new_key' hash key,
that FooBar does not implement a method with the same name as your
accessor name, and are willing to gamble that things will stay that way,
the cheap hack would seem to be inheritance -- create a subclass and
provide a suitable constructor and/or accessor. But, now you need to
provide for two future possible collisions (hash key plus method name).
All of the above is "old school" Perl OO. The new way is Moose:
http://search.cpan.org/~doy/Moose-2.0401/lib/Moose.pm
http://lists.perl.org/list/moose.html
I'm still climbing the Moose learning curve, so I'll refrain from making
any suggestions.
HTH,
David
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/