Another article of the series “Yet Another Perl 6 Operator”
Today’s operator is a very simple one, the string concatenation operator.
my $a = 'ab' ~ 'c'; # 'abc' my $b = 'def'; my $c = $a ~ $b; # 'abcdef'
The operator is now written '~' and not '.' as in Perl 5. Think of it as “stitching” the two ends of its arguments together (S03).
Appending to a string can likewise be done with '~='. (Compare to '+=' that adds to a number.)
$c ~= "xyz"; # 'abcdefxyz' my $d = $a; $d ~= $b; # 'abcdef'
The infix operator '~' keeps the same precedence as '+' in Perl 6. The tilde is consistently employed for string operations and fits nicely with the unary '~' that coerces its argument to strings and the prefix '~' (used in operators like '~|') to mean bitwise operations on arguments interpreted as long bit strings.
String interpolation is still built on terms of string concatenation, meaning "Answer = $answer\n" is equivalent to 'Answer = ' ~ $answer ~ "\n".
The compatibily break with Perl 5 was a rather important one, since '.' was after a few years of common OO practice the operator of choice to join together the invocant object and a member (attribute, method, etc.). The repurposing was a long awaited thing.
At the other hand, in Perl 5, '~' is no longer the bitwise not. If you look closer to Perl 6 design, you’ll see it is filled with these breaks with traditions for the sake of a greater cause: consistency.
Friday (September 21, 2007) will bring you the next article of the series.
More thanks to the folks at #perl6 (at irc.freenode.net) and at the perl6-language@perl.org mailing list.
LINKS
- “Changes to Perl 5 operators” at Synopsis S03, the official source
- The introduction of this series
- Official Perl 6 Documentation
- Perl 6 in your browser
- Everyday Perl 6, an article by Jonathan Scott Duff


yuck
Perl 6 looks worse and worse every day.
@Bryan
I hope not everyone disdains Perl 6 on such grounds: because it looks worse without telling what seemed to have gone wrong.
I do believe Perl 6 will be a great language that will attract many developers due to mixing the powerful pragmatism of Perl 5 and a lot of reformed aspects which reject many of the typical moans on Perl 5 about inconsistencies and ugly hacks.
seriously... =~ and ~= having different meanings.. Wow. worst concatenation operator, ever.
@dextius
The point is that there is no '=~' operator anymore. Instead, there is '~~' (the smart matching operator) and we'll get into this soon. So there are infix '~' (for string concatenation), '=~' (for string appending), '~' (for string coercion, instead of '""' used in Perl 5 overload and not available explicitly as syntax), '~|', '~&', '~^' (for bitwise operations on bit strings), etc. This is far more consistent -- but ok, it may take time to get used to.
By the way, the operators '=>' and '>=' have very different meaning also, and it is not such a big problem.
oh, you mean => aka. the operator that means the same thing as "," ? I swear I remember Larry posting his regrets of the arrow operator.
Sorry for the curt reply, I was also unaware of '~~' That makes is a solid change.
But for concatenation, some languages use "&", others use "+", perl5 used "."... I thought I read that if perl6 did change, it would move TOWARD another language, not further away)..
And you're wise for bringing up the bitwise operators, as they don't make the water any clearer..
There is not the case anymore. '=>' is a pair constructor in Perl 6 and no longer only a synonym for ','.
You have to consider the whole like Larry did and has being doing in Perl 6 design. "&" does not fly because C tradition suggests "&" for and, "|" for or and "^" for xor. "+" is not reasonable for Perl because of automatic coercions between strings and numbers. '1'+2 does the right thing, and there must exist a way to concat and there is: '1'~2. Other languages just don't let you do that: you are obliged to explicitly coerce. The '.' will serve a greater cause as mentioned in the article.
The consistency of using '~' for string operations is reforced by using '+' for numeric and '?' for boolean operations.
As I said, you have to consider the whole to understand the sense of these new operators and changes.
This does in fact make things clearer since ~ now always means
string-something, unary ~ stringifies its argument, binary ~ concats
strings, operators that start with ~ do their respective operations in
string context (e.g. ~|) etc.