Hello,

I recently encountered a bug in my code which took me quite a while to track down. I've created a minimal test case which demonstrates the problem. The tests were conducted with Perl 5.8.6 on Darwin and the original error was with 5.8.8 on FreeBSD.

The following code should be saved as Example/Foo.pm.

package Example::Foo; use strict; use warnings; use Example::Foo::One; my $obj = new Example::Foo; $obj->One(); sub new { my $self = shift; return bless {}, $self; } sub One { my $self = shift; my @caller = caller(); print "Example::Foo::One called, caller is $caller[0], line $calle +r[2]\n"; print "...Ref self is: ".(ref $self)."\n"; $self->Two(); } sub Two { my $self = shift; my $one = new Example::Foo::One('arg' => 'value'); ## Line of int +erest } 1;
And this should be saved as Example/Foo/One.pm:
package Example::Foo::One; use strict; use warnings; sub new { my $self = shift; print "Calling Example::Foo::One::new\n"; return bless {}, $self; } 1;
If you run this code, you will see that instead of calling Example::Foo::One::new(), the line in question calls Example::Foo::One(), with the error message 'Can't locate object method "Two" via package "arg" (perhaps you forgot to load "arg"?)', since it has shifted off the first argument to use as $self.

If the syntax of the line is changed to this:

my $one = Example::Foo::One->new('arg' => 'value');
the same error occurs (albeit with a different error message).

However, if you change the syntax to the very explicit:

my $one = Example::Foo::One::new('Example::Foo::One', 'arg' => 'value');
the correct method is called.

I am wondering why the original syntax did not call Example::Foo::One::new(), but instead called Example::Foo::One().


In reply to 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.