perl - How do you get MotherDogRobot to birth an array of puppy objects using map and a hash of hashes? -
puppy meta data gets read in config file using (general::config) , creates hash of hashes
$puppy_hashes = { puppy_blue => { name => 'charlie', age => 4 }, puppy_red => { name => 'sam', age => 9 }, puppy_yellow => { name => 'jerry', age => 2 }, puppy_green => { name => 'phil', age => 5 }, }
the motherdogrobot package consumes puppies hash birth array of puppy objects (lol)
package motherdogrobot; use moose; use puppy; use data::dumper; #moose includes warn , strict sub init_puppy{ my($self,%options) = @_; $puppy = puppy->new( %options ); return ($puppy); } sub birth_puppies{ my($self,$puppy_hashes) = @_; @keys = keys %{$puppy_hashes}; @puppies = map { $self->init_puppy( $puppy_hashes->{$_} ) } @keys; return(@puppies); } sub show_me_new_puppies{ my($self,$puppy_hashes) @_; print dumper($self->birth_puppies($puppy_hashes)); }
error odd number of arguments
passing %options puppy->new(%options)
no luck birthing puppies -- means can't put lasers on heads =/
update
i think problem i'm passing hash ref init_puppy() instead of array or hash, when try pass %options new constructor, it's not getting proper ( key => value) pair -- hence odd number of arguments error.
but standpoint i've been looking @ code long cant figure out how dereference properly.
btw official day 22 of using perl!
you're using empty variables if they're not empty, is, you're not doing @
print "hi $_ " @foo;
this assumes incomplete snippet you've shown you're using
update: in sub init_puppy, never initialize my($self,%options)=
@_;
#!/usr/bin/perl -- use strict; use warnings; main( @argv ); exit( 0 ); sub main { $puppy_hashes = { puppy_blue => { name => 'charlie', age => 4 }, puppy_red => { name => 'sam', age => 9 }, puppy_yellow => { name => 'jerry', age => 2 }, puppy_green => { name => 'phil', age => 5 }, }; $puppy ( motherdogrobot->birth_puppies($puppy_hashes) ) { print join ' ', $puppy, $puppy->name, $puppy->age, $puppy->dump, "\n"; } } begin { package puppy; begin { $inc{'puppy.pm'} = __file__; } use any::moose; has 'name' => ( => 'rw', isa => 'str' ); has 'age' => ( => 'rw', isa => 'int' ); package motherdogrobot; begin { $inc{'motherdogrobot.pm'} = __file__; } use moose; use puppy; sub init_puppy { ( $self, %options ) = @_; $puppy = puppy->new(%options); return ($puppy); } sub birth_puppies { ( $self, $puppy_hashes ) = @_; @puppies = map { $self->init_puppy( %{$_} ) } values %$puppy_hashes; return (@puppies); } no moose; }
Comments
Post a Comment