On Tue, 29 Oct 2002, Damian Conway wrote:

> Or one could define a copy-the-invoke method call operator (say, C<+.>):

As a rule I prefer to see "safe" operations have short names and
"dangergous" operations with longer ones.  In this context that means "copy"  
gets the short name and "in place" gets the longer one.

So we should define that methods where possible return new values rather
than acting in-place, but define an apply-assignment operator (say C<.=>) to
use when you want to act in-place.  Of course, the methods can be made aware
of this so that they can optimise where appropriate.

  $str .= chomp;          # store result in-place
  $new = $old . chomp;    # return resultant value

  $str .= lcfirst;        # store result in-place
  $new = $old . lcfirst;  # return resultant value

  $str .= replace( /foo/ -> {'bar'} );
  $new = $old . replace( /foo/ => { 'bar' } );

Unfortunately that sits badly with things that "return" two non-scalars
(such as "splice" resulting in an portion excised and a remainder) because
it means that this assignment operator doesn't return the "resulting value"  
the way that other assignment operators do.

Also having the in-place version return something different from the copy 
version would be non-obvious to neophites, but that's a problem no matter 
what notation is used.

  @excised = @array .= splice($position,$count,@insertions);

vs

  @excised = @array . slice($position,$count);
  @unexcised = @array . splice($position,$count,@insertions);

So, in

  @a = @b .= grep {/foo/};

should @a be the elements that I<do> contain "foo" or those that I<don't>?

-Martin


Reply via email to