Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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 chilling in the Monastery: (6)
As of 2024-04-18 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found