Wrong package used when method and package have the same nameby Anonymous Monk
|on May 02, 2006 at 13:51 UTC||Need Help??|
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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.
And this should be saved as Example/Foo/One.pm:
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:
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().