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

Wrong package used when method and package have the same name

by Anonymous Monk
on May 02, 2006 at 13:51 UTC ( #546890=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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().

Replies are listed 'Best First'.
Re: Wrong package used when method and package have the same name
by japhy (Canon) on May 02, 2006 at 14:09 UTC
    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
      Thanks for the information. I guess I should not have called it a minimal test case but simply a reduced test case.

      Usually my method names would be lower-case and my package names ucfirst, so the situation would never come up. In this case I was attempting to replace an existing module and retain the same method names, which happened to use this a mixed case capitalization scheme. The duplicate names were introduced when I decided some of the data shouldn't really be stored in the original module, because multiple instances would need to be stored, and the logical name for the holding class was of course the same as the original method name...

Re: Wrong package used when method and package have the same name
by ikegami (Pope) on May 02, 2006 at 14:13 UTC

    It's a known limitation of the indirect method call syntax. Instead of
    new Example::Foo::One('arg' => 'value')
    use
    Example::Foo::One->new('arg' => 'value')

    Update: Thanks blazaar and davorg. It's easy to miss things when one has just woken up.

    I think changing
    Example::Foo::One
    to
    Example::Foo::One::
    will do the trick.

    >perl -le "$c = Example::Foo::One::; print $c" Example::Foo::One

      That's was my initial thought too. But then I tried it and found that it still didn't work correctly (although it failed in a different manner). And, in fact, the original poster had already tried that to.

      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).

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        aye, I've just realized that. I was on my way to update my post.
Re: Wrong package used when method and package have the same name
by eff_i_g (Curate) on May 02, 2006 at 14:57 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://546890]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-11-27 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?