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

I'm having a problem with Class::MethodMaker object_list forwarded methods. Maybe I just can't get the syntax right. Can anyone can point me to a working code example or provide some insight? Or maybe ideas about peeking into the C::MM generated methods? My broken code and output follow the POD excerpt.

From POD:

object_list
Functions like list, but maintains an array of referenced objects in each slot. Forwarded methods return a list of the results returned by maping the method over each object in the array.
Arguments are like object.
use strict; use Class::MethodMaker "-sugar"; package MyPackage::Item; make methods new_hash_init => 'new', get_set => [qw/ name /], ; # make methods package MyPackage::List; make methods new_hash_init => 'new', object_list => [ MyPackage::Item => { slot => 'items', comp_mthds => [qw/ name /], }, ], ; package main; use Data::Dumper; my $list = MyPackage::List->new(); $list->items_push( MyPackage::Item->new(name=>'test1') ); print Dumper($list); # POD says this should return a list from a map # of the forwarded methods over all objects in # the list, but I can't get it to work! print $list->items->name;
Output:
$VAR1 = bless( { 'items' => [ bless( { 'name' => 'test1' }, 'MyPackage::Item' ) ] }, 'MyPackage::List' ); Can't call method "name" on unblessed reference at test.pl line 32.
--
The ability to destroy planets is insignificant next to the power of the Deacon Effect.

Replies are listed 'Best First'.
How to 'Do The Work' (was Re: Class::MethodMaker object_list help needed)
by Solo (Deacon) on Feb 03, 2003 at 19:35 UTC
    Since I hate questions without answers and nodes without conclusions--here are my findings.

    I was being an idiot. However, I think the manner in which I discovered this Simple Truth is worthwhile reading. First, the answer.

    In the example code above, the correct syntax for calling the forwarded name method for the MyPackage::List object is as follows.

    print $list->name; # NOT: print $list->items->name

    Of course, this seems obvious with hindsight. It's a method of the $list object! I think I was trapped in a VB mindset and couldn't see the obvious Perlish use... or something.

    Anyway, on to the important bit.

    How I figured this out--aka How to 'Do The Work'

    1. I read the POD.
    2. I read the POD again.
    3. I looked at the module source*
    4. I used Super Search to look for "MethodMaker".
    5. I used Google to look for "MethodMaker".
    6. I spent a long time finding every pod2html mirror in existence.
    7. I got smart and Googled the comp.lang.perl groups.
    That's when I found a nice thread about exploring the symbol table--and knew that was where I could find the name (hence, usage) of the Class::MethodMaker method. Dominus posted the code I used.

    Moreover, I found a wealth of other information. For instance, "-sugar" probably shouldn't be used... and a long list of reasons that I should use Class::MakeMethods instead.

    Now, if I could only get Class::MakeMethods 1.006 to build under Win32... **sigh** ...back to work!

    --
    * The point in time that I scratched my head and called for backup.

    May the Source be with you.

      For what it's worth, the problem with Class::MakeMethods 1.006 on Windows was just a "make test" failure (see node 276489) -- the module itself worked correctly -- and that problem should be fixed in the new 1.008 release.