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
    package Child;
    use base 'Parent' ;
    sub data { return { one => 1, two => 2 } }

    Yes, that's the way I've thought of (see my answer to Joost)... But perhaps I'll dig in the deeper mysteries of tie instead :)
    By the way there must be a name for what I'm trying to do, is it "object introspection"?

      By the way there must be a name for what I'm trying to do, is it "object introspection"?

      More like "breaking encapsulation".

      The whole point of classes and objects is polymorphism. That is, given an object, you don't care what its specific type is as long as it supports the operations you want to perform on it in a meaningful way.

      In your specific case, there's a perfectly good way to get that instance data without relying on package variables or symbolic references or even caring about the specific type of the class. That way is calling a method.

      With only a couple of classes, it might look like you're duplicating data by overriding methods. Maybe so... but it takes little imagination to see an expanded system with more subclasses, some of which can reuse their parent data (and good luck coding around that by accessing package variables symbolically!) and others that can override the parent method to call it and add, delete, or modify just one particular field in the results.

      You're making a lot more work for yourself trying to work against the object system. Embrace it!

        But in that particular case, my objects represent data files that work exactly the same way (same methods all along) but carry different data. There isn't any method in any of the "children" classes : each children class really represent a "polymorph version" of the parent class, which contains all meaningful behaviours. And actually there isn't anything in my code that rely on this or that class : except of course when reading or writing the target file, I have to read it the right way, or feed it with the right data. The data is polymorphic, not the behaviour.
        Would I avoid that call from the parent to the child to call data, I'd need to duplicate most method exactly (there aren't any differences!) in all the classes. Wouldn't it be stupid and unmaintanable?
        I really don't think it can be called "breaking encapsulation", because nothing in the parent classes rely on what is inside the child class.
        Please shed some light on me if you feel differently...