in reply to displaying package variable from inherited method.
This doesn't DWIM, $obj->show() returns nothing. It works of course if I move the show method to Child.pm, but I'd need to duplicate subs into all children, which is a sin. What magic am I missing ?
Your missing that the package var referenced by the method is $data in the package proto. You want something like:
sub show { my $self = shift ; my $proto = ref $self ; my $val = eval "\$${proto}::data"; print $val; print Dumper $val }
or, to avoid all that evil messing about with package global variables and associated hassles with the cases where you might want sub-classes of your children to inherit the same $data why not just do it like:
{ package Parent; use Data::Dumper ; sub new { my $proto = shift ; return bless {}, $proto } sub show { my $self = shift ; my $val = $self->data; print Dumper $val } } { package Child; use base 'Parent' ; sub data { return { one => 1, two => 2 } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: displaying package variable from inherited method.
by wazoox (Prior) on Jun 10, 2006 at 15:04 UTC | |
by chromatic (Archbishop) on Jun 10, 2006 at 18:09 UTC | |
by wazoox (Prior) on Jun 10, 2006 at 20:26 UTC | |
by chromatic (Archbishop) on Jun 10, 2006 at 22:55 UTC | |
by wazoox (Prior) on Jun 11, 2006 at 12:57 UTC |