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

print map{{title=>$_->title(),desc=>$_->descr()}} Chare->get('duedate' +,'2002-09-22 ');
Chare->get(); returns a list of objects, with methods. The above code gives me this error:
Can't locate object method "descr" via package "title" (perhaps you forgot to load "title"?)
However, when i just do this:
print map{{title=>$_->title}} Chare->get('duedate','2002-09-22 ');
Then it works perfectly. .. Also note that reversing the terms and changing it to $_={};$_; has no effect. I'm stumped

What DWIM feature am i tripping over here?

Replies are listed 'Best First'.
Re: Evil bug: map + anony hash
by PodMaster (Abbot) on Sep 24, 2002 at 05:33 UTC
    Works fine for me.
    package Car; sub new { bless {},shift }; sub title { 'BITTER' }; sub descr { 'BUTTER' }; sub F { ( Car->new, Car->new, Car->new, Car->new ) }; package main; print map{{title=>$_->title(),desc=>$_->descr()}} Car->F(); use Data::Dumper; die Dumper map{{title=>$_->title(),desc=>$_->descr()}} Car->F(); __END__ HASH(0x1ab51f0)HASH(0x1ab5280)HASH(0x1ab5310)HASH(0x1ab53a0)$VAR1 = { 'title' => 'BITTER', 'desc' => 'BUTTER' }; $VAR2 = { 'title' => 'BITTER', 'desc' => 'BUTTER' }; $VAR3 = { 'title' => 'BITTER', 'desc' => 'BUTTER' }; $VAR4 = { 'title' => 'BITTER', 'desc' => 'BUTTER' };
    You should take the mystery out of the questions when you can (like with this Chars->get business).

    What makes a bad question? Before You Post ... How (Not) To Ask A Question How to ask questions the smart way

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Evil bug: map + anony hash
by fglock (Vicar) on Sep 24, 2002 at 14:23 UTC

    It might be a problem with $_ localization: look at line 3.

    package Car; sub new { bless {},shift }; sub title { $_='title'; 'BITTER' }; sub descr { 'BUTTER' }; sub F { ( Car->new, Car->new, Car->new, Car->new ) }; package main; print map{{title=>$_->title(),desc=>$_->descr()}} Car->F(); use Data::Dumper; die Dumper map{{title=>$_->title(),desc=>$_->descr()}} Car->F();

    output:

    Can't locate object method "descr" via package "title" (perhaps you forgot to load "title"?) at test.pl line 8.
      BING BING BING!! We have a winner! That was exactly the problem (and solution) i rewrote the methods that were being called to avoid using $_, and it worked like a charm. Hmph, that has to be a bug or something.

        Oh. That's not a bug - $_ is a global so if you stomp on it then you have to deal with it. The thing is - since $_ is a global just local()ize it and it'll lose your stomping.

Re: Evil bug: map + anony hash
by bart (Canon) on Sep 24, 2002 at 15:29 UTC
    The above code gives me this error: Can't locate object method "descr" via package "title" (perhaps you forgot to load "title"?)
    I dont think there's any hidden magic here. This simply means that Perl can't find a method with name "descr" for your objects of class "title". I assume that is indeed the class for your objects? Apparently, the method "title" does exist.

    So... is there a sub "descr" in package "title"?