Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: current/active package

by eye (Chaplain)
on Sep 25, 2009 at 06:04 UTC ( #797399=note: print w/ replies, xml ) Need Help??


in reply to Re: current/active package
in thread current/active package

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.


Comment on Re^2: current/active package
Select or Download Code
Re^3: current/active package
by ikegami (Pope) on Sep 25, 2009 at 06:06 UTC
    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();
Re^3: current/active package
by rovf (Priest) on Sep 25, 2009 at 07:51 UTC
    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>
      In the example, there are no objects. This is supposed to be invoking a package-level method with ISA inheritance, knowing the package you are in, and the package the sub was invoked upon. I realize that in the example, $_[0] = the package the sub was invoked upon. But aren't there other ways to invoke the function in which that will not be true? What about bar::woot() ???

        But aren't there other ways to invoke the function in which that will not be true?

        Yes. The only difference between a subroutine call, a static method call, a virtual object method call and a non-virtual object method call is in how the sub is called. If it's called improperly, it's a bug, and it will result in buggy behaviour.

        What about bar::woot() ???

        bar::woot() doesn't exist, so you'll get a run-time error.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://797399]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-08-01 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (36 votes), past polls