On Apr 27, 2010, at 3:36 AM, Shlomi Fish wrote:
Hi all!

After merging XML-Grammar-Fiction and XML-Grammar-Screenplay, I have
accumulated several questions about Moose, so I'd post each one in a separate
post to keep each thread single-topic. (I hope it's OK.)

The first one is how to implement a Class::Std/Perl 6-like walkmeth:

* http://blog.gmane.org/gmane.comp.lang.perl.qotw.discuss/month=20070701

* http://search.cpan.org/perldoc?Class::Std (search for CUMULATIVE).

What it does is that in each class out of the various inheritance tree of the
module, there is a method, and one accumulates their results so if:

[pseudo-code]
package Base1;

sub a1
{
        return [qw(foo)];
}

package Base2;

sub a1
{
        return [qw(bar)];
}

package Class;

extends('Base1', 'Base2');

sub a1
{
        return [qw(quux)];
}
[/p-code]

Then accumulating a1 will yield [qw(foo bar quux)].

I've implemented something similar in Test-Run:

http://svn.berlios.de/svnroot/repos/web-cpan/Test-Harness-
NG/trunk/modules/Test-Run/lib/Test/Run/Base.pm

(short URL - http://xrl.us/bhjhgt ).

Quoting from it:

[code]
sub accum_array
{
   my ($self, $args) = @_;

   my $method_name = $args->{method};

   # my $class = ((ref($self) eq "") ? $self : ref($self));

   my @results;
   foreach my $isa_class (
       $self->meta->find_all_methods_by_name($method_name)
   )
   {
       my $body = $isa_class->{code}->body();
       push @results, @{ $self->$body() };
   }

   return \...@results;
}
[/code]

My question is: is there a better way to do it using Moose? (Or one of the MooseX modules?) Instead, should I implement my own private logic or create a
new MooseX module?

No, that is pretty much how you would do it. Take a look at Moose::Object::BUILDALL, it does the same thing.

- Stevan




Reply via email to