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
In reply to Re: Question about __PACKAGE__
by ikegami
in thread Question about __PACKAGE__
by sman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |