Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here is a much simpler case:
package DEF; sub new { bless [], shift } package ABC; sub new { bless {}, shift } sub DEF { "ABC's DEF function" } # still in package ABC! $x = new ABC; $y = new DEF; $z = new DEF::; print "X=$x\nY=$y\nZ=$z\n"; __END__ X=ABC=HASH(0x8151d6c) Y=ABC's DEF function=HASH(0x8151b44) Z=DEF=ARRAY(0x81525c4)
The problem is that, while in the ABC package, Perl sees new DEF as two function calls, not as an indirect object call. This is simply because of the order that Perl tries things in -- since it knows 'new' and 'DEF' are declared functions at that point, that's how it treats both of them. By using new DEF::, we force Perl to interpret it as an object call. Sadly, not even DEF->new will work, because Perl will try calling the "new" method of the package name returned by the DEF() function, "ABC's DEF function"!

Long story short, this is just how Perl parses your code. You shouldn't give functions and packages identical names. (Indirect object notation is smelly and should be avoided anyway.) To be foolproof, always append "::" to the end of your class names. DEF::->new and new DEF:: both behave properly in this case.


Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

In reply to Re: Wrong package used when method and package have the same name by japhy
in thread Wrong package used when method and package have the same name by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found