I seek your wisdom in determining the best way to access a package variable of a subclass from within a base class method. I have identified a working solution, but it is ugly and I don't completely understand it. It also uses stringy eval, which I know to be taboo in most cases.
Surely there must be a more elegant (or correct) way for a method in a base class to access the package variables of the subclass that inherited it?
Thanks in advance, and here is some functional example code illustrating what I am trying to accomplish.
m.
#================= package BaseClass; #================= our $ID = 1; sub new { my $class = shift; return bless {}, $class; } sub id { my $self = shift; my $class = ref $self; return eval "\$${class}::ID"; } #================ package SubClass; #================ use base qw( BaseClass ); our $ID = 2; #============ package main; #============ my $obj = new SubClass; my $id = $obj->id(); print "$id\n";
In reply to Access package variables of subclass from base class by slower
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |