in reply to Access package variables of subclass from base class

In C++ parlance you should use a 'pure virtual' member function. In Perl that just means that you assume the derived class will provide a member. Consider:

use strict; use warnings; package BaseClass; sub new { return bless {}, shift; } sub useVirtualX { my ($self) = @_; return $self->GetX (); } 1; package main; use base 'BaseClass'; my $obj = main->new (); my $privateX = 42; print $obj->useVirtualX (); sub new { return shift->SUPER::new (); } sub GetX { return $privateX; }

Prints:

42

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Access package variables of subclass from base class
by Anonymous Monk on Mar 04, 2009 at 12:36 UTC
    sub GetX { my ($self) = @_; my $subc = ref($self) ; if ( $subc ne __PACKAGE__ ) { croak "do not call $subc\::SUPER->GetX"; } else { croak "GetX must be implemented by subclass"; } }