YAP6 Operator: Stitching

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'

8 Comments

Bryan
2007-09-19 16:56:02

yuck

Perl 6 looks worse and worse every day.

Adriano Ferreira
2007-09-20 08:18:45

@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.

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.

Adriano Ferreira
2007-09-20 09:24:21

seriously... =~ and ~= having different meanings

By the way, the operators '=>' and '>=' have very different meaning also, and it is not such a big problem.

dextius
2007-09-20 09:47:41

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..

Adriano Ferreira
2007-09-20 10:04:21

oh, you mean => aka. the operator that means the same thing as "," ? I swear I remember Larry posting his regrets of the arrow operator.

There is not the case anymore. '=>' is a pair constructor in Perl 6 and no longer only a synonym for ','.

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)..

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.

And you're wise for bringing up the bitwise operators, as they don't make the water any clearer..

As I said, you have to consider the whole to understand the sense of these new operators and changes.

Ævar Arnfjörð Bjarmason
2007-09-20 10:04:56

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.

Sign up today to receive special discounts, product alerts, and news from O'Reilly.