* Tom Browder <tom.brow...@gmail.com> [2015-08-14 14:10]:
> On Fri, Aug 14, 2015 at 6:48 AM, Philip Hazelden
> <philip.hazel...@gmail.com> wrote:
> > Correct me if I'm wrong, can't you do
> >
> > $s .= trim
> Um, I saw that usage in an earlier thread but didn't try it because it
> didn't "look right" given my experience with the ".=" operator as I'm
> used to it. (Note I have now seen that behavior clearly documented at
> <http://doc.perl6.org/language/operators>.)

Maybe because in Perl 5 that would mean what is written in Perl 6 as

    $s ~= trim

which itself “looks wrong” in turn. The whole getup take some getting
used to.

But remember that . in Perl 6 is the method call operator. Then it makes
sense if you follow the logic of mutating meta-ops:

    $s = $s + 1;       ==>     $s += 1;
    $s = $s - 1;       ==>     $s -= 1;
    $s = $s .trim;     ==>     $s .= trim;

But yeah, it still looks weird to me too. I’ve understood it rationally
but it’ll be a while before I stop second-guessing myself for a split
second every time I write it.

> Note that the same behavior applies to the 'substr' string method so
> that begs the question of why is the 'substr-rw' method justified and
> 'trim-rw' not? It seems at first glance that the 'substr-rw' method
> should be removed.

They would not be at all similar, though: substr-rw doesn’t modify the
value it’s being called on. This code does absolutely nothing:

    $s.substr-rw(1, 1);

What substr-rw does is give you a reference to part of the string. *You*
can then modify that, e.g.

    $s.substr-rw(1, 1) = 'x';

That’s totally different from how your proposed .trim-rw would work. For
that need, the .= mutating method call operator is the generic solution,
which obviates the endless writing of both a .trim and a .trim-rw, and
so on, for every single method. (Or .sort vs .sort! etc etc ad nauseam…)

Aristotle Pagaltzis // <http://plasmasturm.org/>

Reply via email to