[LA.pm] Parallel::Simple

Ofer Nave ofer at netapt.com
Sun Feb 27 15:26:40 PST 2005


(BTW-Having people actually looking at and commenting on my code is 
exhilirating.  I can see contributing to CPAN becoming an addiction. :)

On Sun, 27 Feb 2005, Kevin Scaldeferri wrote:

> On Feb 27, 2005, at 1:21 PM, Ofer Nave wrote:
> 
> >
> >> You probably ought to initialize $error and $return_values.  Otherwise
> >> you might have some problems if someone did something odd like 
> >> run()ing
> >
> > No reason to.  I would only initiliaze them to undef to signify that 
> > they
> > have not yet been defined, but perl does this to new variables
> > automatically.
> >
> 
> Won't errplus() fail trying to dereference an undef value?  Also, with 
> warnings on, it could complain about interpolating $error if it is 
> undef.

Yes, good catch.  I should detect and return undef in the errplus method.  
If prun has never been called, then any error or return values will be 
undefined, so returning undef in that case seems correct.

> Actually, looking more closely at this, I notice that $return_values is 
> sometimes a hash ref and sometimes an array ref.  I think that this is 
> probably a bad idea, and it also makes it unclear just what you would 
> initialize it to.

Yes, I was trying to be clever and provide return values in the same 
structure that the code blocks were passed in.  It seemed appropriate, 
even if it does violate traditional wisdom.

I could standardize on always returning hashrefs, and using array indices 
as keys when you don't used named code blocks, which would look like this:

my $success = prun(
  sub { print "foo!\n" },
  sub { die( "arrg!" ) },
);

unless ( $success ) {
  my $rv = Parallel::Simple::rv();
  while ( my ( $block, $exit_value ) = each %$rv ) {
    print "$block => $exit_value\n";
  }
}

which would look like this:

0 => 0
1 => 255

But using array indices as hash keys makes me queasy.

I imagine most people will just pass the value of errplus to die and be 
done with it, in which case it all works.  If they want to pay special 
attention to the return value of a particular block, they can do to as 
well, in a natural way:

# Style 1
prun(
  sub { print "foo!\n" },
  sub { die( "arrg!" ) },
) or die( "first child exit with: " . Parallel::Simple::rv()->[0] );

# Style 2
prun(
  foo => sub { print "foo!\n" },
  bar => sub { die( "arrg!" ) },
) or die( "first child exit with: " . Parallel::Simple::rv()->{foo} );

See?

-ofer



More information about the Losangeles-pm mailing list