in reply to how to create a "delegating" array?

I guess you'll need to return another object (untested):

package MapDelay; sub new { my ($class,@self) = @_; bless \@self, $class; }; sub AUTOLOAD { (my $m = $AUTOLOAD) =~ s/.*:://; my $self = shift; map { $_->$m(@_) } @$self }; package Foo; sub foo { return MapDelay->new( Bar->new, Bar->new, Bar->new ); };

Also, Bar->new x3 stores three copies of the same object. I'm not sure if you wanted that.

Replies are listed 'Best First'.
Re^2: how to create a "delegating" array?
by holli (Abbot) on Nov 29, 2009 at 09:21 UTC
    No, I wanted independent instances. Your code got me in the right direction. It was a little buggy, though =). Moosified version:
    package Bar; use Moose; has x => ( is => 'rw', isa => 'Any' ); sub bar { print "bar", shift->x, "\n"; } package MapDelay; use Moose; use vars '$AUTOLOAD'; has elements => ( is => 'rw', isa => 'ArrayRef' ); around BUILDARGS => sub { my ($orig,$class) = @_; $class->$orig(elements => [splice @_,2]); }; sub AUTOLOAD { (my $m = $AUTOLOAD) =~ s/.*:://; map { $_->$m(@_) } @{shift->elements} }; package Foo; use Moose; sub foo { return MapDelay->new( Bar->new(x=>1), Bar->new(x=>2), Bar->new(x=> +3) ); }; package main; Foo->new->foo->bar; #prints bar1\nbar2\nbar3\n


    holli

    You can lead your users to water, but alas, you cannot drown them.
      Bar->new(x=>1), Bar->new(x=>2), Bar->new(x=> +3
      this can be good place to use map:  map Bar->new (x => $_), 1 .. 3