wazoox has asked for the wisdom of the Perl Monks concerning the following question:
Dear Brothers,
I'm losing my Latin. I've made a fake example to illustrate my problem : I have a base class that define methods, and child classes that define the data returned thru package variables. However, it doesn't work as expected, I can't get any data back, probably because I'm missing some step ( guess what : I'm home and my "OO Perl" by TheDamian is at office today ). Here's the code from Parent.pm :
use strict; use warnings; package Parent; use Data::Dumper ; sub new { my $proto = shift ; return bless {}, $proto } sub show { my $self = shift ; my $proto = ref $self ; print $proto::data ; print Dumper $proto::data } 1;
use strict; use warnings; package Child; use base 'Parent' ; our $data = { one => 1, two => 2 } ; 1;
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 ? Of course I could set up the data in a sub or a closure instead of a simple variable, but I'd like to understand what's happening anyway.#!/usr/bin/perl use strict; use warnings; use Child ; my $obj = Child->new(); $obj->show();
|
|---|