Hi all,

I'm sure we've all wished for a concatenation operator that would prepend a string to a string in the same way the .= operator appends.

So why isn't there one?

It's silly that you can write:

$foo .= 'bar';
But not:
$baz =. 'qux';
and instead have to do:
$baz = 'qux' . $baz;

Today I got to wondering if I had missed that such an operator had been introduced in some recent Perl version so I ran the code, and to my surprise Perl said:

Reversed .= operator at -e line 5. syntax error at -e line 5, near "=."
Now, if Perl knows that this particular syntax error is a "reversed .= operator", and not, say, "some new operator I didn't know about" - i.e. the syntax is not in use for anything else - then why isn't it implemented?

Can any guts gurus shed any light?


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re: Reversed .= operator
by LanX (Saint) on Apr 29, 2016 at 16:54 UTC
    =. doesn't follow the rule Operator + Assignment like in += or *= , so many mechanisms would fail like in overload where the overloaded += can be automatically derived from overloaded +

    So you'd either need an explicit prepend operator like ..= or you'd need to define your own helper function sub prepend($$) .

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Reversed .= operator
by choroba (Cardinal) on Apr 29, 2016 at 21:40 UTC
    substr can do that easily:
    substr $string, 0, 0, $prefix;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Yes, of course, there is more than one way to do it. I would use

      $string = $prefix . $string
      over
      substr $string, 0, 0, $prefix
      myself (for readability, not because it's two fewer characters) ... but I want a (built-in) operator, dammit!


      The way forward always starts with a minimal test.
        It's not the same. substr is a bit faster, probably because it doesn't create a new string?

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Reversed .= operator
by Laurent_R (Canon) on Apr 30, 2016 at 09:42 UTC
    Just create the operator:
    > multi sub infix:<=.> (Str $w1 is rw, Str $w2) { $w1 = $w2 ~ $w1;} sub infix:<=.> (Str $w1 is rw, Str $w2) { #`(Sub+{Precedence}|14963240 +0) ... } > > my $c = "bar"; bar > $c =. "foo"; foobar > say $c; foobar
    Oh, but I forgot to say, this is Perl 6, of course.

    Sorry about it, I couldn't resist. ;-)

      So in other words, "Perl6" doesn't have it either.


      The way forward always starts with a minimal test.
        Nope, but it does not really need it either: it takes just one code line to define it.

        Take a look at Perl 6 without prejudice, I'm sure you'll love it.

        Thanks, raiph, I did not know about that reverse argument operator.
Re: Reversed .= operator
by karlgoethebier (Abbot) on May 03, 2016 at 07:38 UTC
    "...prepend a string to a string"

    Seems to be an old debate: What operator prepends?

    Ibidem: "...It wasn't a useful enough operation to merit its own operator"

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»