in reply to How to implement such kind of magic?

I don't quite understand what you want. Should each class have its own %types hash, but somehow fall back to the parent's hash if something is not found? If yes, that can be implemented with methods and SUPER:: calls.

Or do you want one hash in the base class, and populate it along the way? That might look like this:

use strict; use warnings; { package Base; our %types = qw/BASE_TYPE Some::Class/; sub get_type { $types{$_[0]} } } { package Foo; our @ISA = qw/Base/; $Base::types{FOO_TYPE} = q/Another::Class/; } { package Bar; our @ISA = qw/Base/; $Base::types{BAR_TYPE} = q/Yet::Another::Class/; } use Data::Dumper; print Dumper \%Base::types; print Base::get_type('BAR_TYPE'), $/;

Replies are listed 'Best First'.
Re^2: How to implement such kind of magic?
by llancet (Friar) on Dec 03, 2009 at 00:52 UTC
    I want to have only ONE function exists in the root class, and it seems only one OUR variable exists in the root class is not a good solution, because it will be affected by all of its descendants. What I want is:
    Foo: get Base and Foo Bar: get Base and Bar
    but not:
    Foo: get Base, Foo and Bar Bar: get Base, Foo and Bar
    It seems that I have to write a sub recursively traverse through the namespace tree?