in reply to Question about __PACKAGE__
First, let's just make sure you know there's nothing special about __PACKAGE__ for the purpose of this discussion. Think of it as a function that returns the name of the package in which it resides.
I guess line3 is calling a object method but don't see any new() syntax.
A class method. The LHS of the arrow is a class name ('Foo'), not an object. It's funny you should mention new, since it's usually a class method as well.
Foo->new # Calls class method new Foo->hello # Calls class method hello __PACKAGE__->new # Calls class method new __PACKAGE__->hello # Calls class method hello
Looks like line4 is calling a regular subroutine and that's why the error happens.
Correct. hello expects to be called as a class method or an object method, but it's called as a subroutine. As a result, the arguments it receives are different than those it expects.
Under which kind of situation should we use __PACKAGE__ to create an object instead of calling its new() in the caller program?
Never unless you're told to do so for a particular method
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Question about __PACKAGE__
by sman (Beadle) on Jan 22, 2010 at 04:01 UTC | |
by chromatic (Archbishop) on Jan 22, 2010 at 06:34 UTC | |
by ikegami (Patriarch) on Jan 22, 2010 at 07:20 UTC | |
by sman (Beadle) on Jan 22, 2010 at 04:35 UTC | |
by SpiceMan (Sexton) on Jan 22, 2010 at 04:58 UTC | |
by sman (Beadle) on Jan 22, 2010 at 05:36 UTC | |
by ikegami (Patriarch) on Jan 22, 2010 at 07:39 UTC | |
by sman (Beadle) on Jan 22, 2010 at 22:00 UTC | |
| |
by sman (Beadle) on Jan 22, 2010 at 22:26 UTC | |
|