toddfreed has asked for the wisdom of the Perl Monks concerning the following question:

package foo; sub woot { print __PACKAGE__; # <-- prints "foo"; print < INSERT ANSWER >; # <-- prints "bar"; } package bar; our @ISA = qw|foo|; package main; bar->woot();

Replies are listed 'Best First'.
Re: current/active package
by ikegami (Patriarch) on Sep 24, 2009 at 23:04 UTC

    ref

    sub method { my ($self) = @_; print ref($self), "\n"; }

    It's very rare to need that. Why do you ask? Maybe there's a better answer if we knew the bigger picture.

      Except,
      sub woot { my ($self) = @_; print __PACKAGE__, "\n"; # <-- prints "foo" print ref($self), "\n"; # <-- prints nothing }
      While,
      sub woot { my ($self) = @_; print __PACKAGE__, "\n"; # <-- prints "foo" print $self, "\n"; # <-- prints "bar" }
      I think this is because, in this simple example, the object was never blessed. Had it been blessed, I think ikegami's answer is correct.
        yeah, oops.
        sub class_method { my ($class) = @_; print "$class\n"; } sub object_method { my ($self) = @_; my $class = ref($self); print "$class\n"; } Class->class_method(); $obj->object_method();
        the object was never blessed.
        There is no object at all. Note that the OP's call of woot is done like this:

        package main; bar->woot();
        This means that he is writing a package name to the left of ->, i.e. woot is might be something like a constructor. So you don't have an object, but you do have a package name.

        -- 
        Ronald Fischer <ynnor@mm.st>