On Thu, Mar 04, 2004 at 04:05:21AM +0000, [EMAIL PROTECTED] wrote:
> Andrew Pimlott wrote:
> > $set->iterate( sub { myfunc($_); 1 } );
>
> The result would be: $set = [ 1 ]
> because that's the _only_ returned value.
Boy, that's confusing to me. I didn't realize (because I didn't read
the documentation fully) that the return value of the callback replaces
the element on which it is called. This is not what I would expect from
a method called "iterate"; I would expect iterate to ignore its return
value, so that the set would only be modified if $_[0] is modified. I
find your examples confusing for this reason:
sub callback {
$_[0]->add( hours => 1 );
}
looks as though it modifies the elements because ->add is destructive.
In fact, it "also" modifies the elements via the return value. I think
the following example would more clearly demonstrate the behavior of
->iterate.
sub callback {
$_[0]->clone->add( hours => 1 );
}
Also, this makes it cumbersome to use ->iterate without modifying the
set, because you'd have to do
sub callback {
myfunc($_[0]);
$_[0];
}
At very least the documentation could be more explicit. The first
sentences could be,
This method modifies a set by applying a callback function to each
element. The element is replaced by the return value of the
function unless the function return sundef, in which case the
element is removed.
Andrew