in reply to Re^2: displaying package variable from inherited method.
in thread displaying package variable from inherited method.

In that example you're not actually sharing the data: you're creating a new hash every time data() is called. If you really want to share the data, you can do:

my $data = { one => 1, two => 2 }; sub data { return $data; }

I don't know how you'd use tie() to get the original code to work. If you really want that construct, you'll need to use symbolic references or - even more dangerous - eval.

Also note that even if it would work as you expected, the original code will break as soon as someone subclasses Child.

update: in the example you posted here, there's no need to do

my $proto = ref $self; print $proto->data;
You can just call
print $self->data;

Replies are listed 'Best First'.
Re^4: displaying package variable from inherited method.
by wazoox (Prior) on Jun 10, 2006 at 15:21 UTC
    In that example you're not actually sharing the data: you're creating a new hash every time data() is called.

    How unfortunate I am ! I can't have all my bad code reviewed by your sharp eyes, precious friend. Thanks a lot for this one :)

    update: in the example you posted here, there's no need to do
    my $proto = ref $self;
    print $proto->data;

    Yes, this one is just a hurried copy-and-paste :)