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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|