in reply to checking a method is not subclassed

You can inspect the value you get from ref($self). If that's not Baz, some other package is calling the method through inheritance (or some other wacky thing). From there, decide what you want to do.

--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Replies are listed 'Best First'.
Re^2: checking a method is not subclassed
by ikegami (Patriarch) on Feb 02, 2006 at 15:51 UTC

    For example,

    package Baz; sub new { my ($class) = @_; return bless({}, $class); } sub example { my ($self) = @_; if (ref($self) ne __PACKAGE__) { require Carp; Carp::croak( "Method &" . __PACKAGE__ . "::example must not be inherited" ); } print(ref($self) . " ok\n"); } package Baz::Foo; @ISA = 'Baz'; package main; Baz ->new()->example(); # ok. Baz::Foo->new()->example(); # dies.

    outputs

    Baz ok Method &Baz::example must not be inherited at !.pl line 28

    It's possible to circumvent this by re-blessing the object.