in reply to Who am I? Inheritance question

If the "generic method" you have in mind is a class method it should start something like:
sub generally_useful { my $class = shift; # ... }
That $class will be the invoking class, not necessarily the __PACKAGE__ where generally_useful lives. This works for constructors.

If the method is an instance method, it will be more like:

sub instance_meth { my $self = shift; my $class = ref $self; }

Phil

Update: Added instance method example.

Replies are listed 'Best First'.
Re^2: Who am I? Inheritance question
by dragonchild (Archbishop) on Mar 16, 2006 at 22:08 UTC
    sub instance_meth { my $self = shift; my $class = Scalar::Util::blessed $self; }

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Does this still work if you want $self to lie to instance_meth about what class it is? Can a mock object still be passed in here? I know ref() is overrideable but it's painful. How does a person handle that with Scalar::Util::blessed?

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Mock objects respond better to blessed() than ref(). Being an mock object implies that it's blessed in the class that it's mocking.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?