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 } } }
In reply to Re: displaying package variable from inherited method.
by adrianh
in thread displaying package variable from inherited method.
by wazoox
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |