(By masak and moritz)
Newcomers to the Perl programming language, version 5, often complain that
they can’t reverse strings. There’s a built-in reverse function, but it doesn’t seem to work at first glance:
$ perl -E "say reverse 'hello'" hello
When such programmers ask more experienced Perl programmers, the solution is quickly found:
reverse has actually two different operation modes. In list context it reverses lists, in scalar context it reverses strings.
$ perl -E "say scalar reverse 'hello'" olleh
Sadly this an exception from Perl’s usual context model. For most operators or functions the operator determines the context, and the data is interpreted in that context. For example
* work on numbers, and
. works on strings (concatenation). So a symbol (or function name, in the case of uc) stands for an operation, and provides context. Not so
In Perl 6, we try to learn from previous mistakes, and get rid of historical inconsistencies. This is why list reversal, string reversal and hash inversion have been split up into separate built-ins:
# string reversal, sorry, flipping: $ perl6 -e 'say flip "hello"' olleh
# reversing lists # perl6 -e 'say join ", ", reverse <ab cd ef>' ef, cd, ab
# hash inversion perl6 -e 'my %capitals = France => "Paris", UK => "London"; say %capitals.invert.perl' ("Paris" => "France", "London" => "UK")
Hash inversion differs from the other two operations in that the result is of a different type than the input. Since hash values need not be unique, inverting a hash and returning the result as a hash would either change the structure (by grouping the keys of non-unique values together somehow), or lose information (by having one value override the other ones).
Instead hash inversion returns a list of pairs, and the user can decide which mode of operation they want for turning the result back into a hash, if that’s necessary at all.
Here’s how you do it if you want the hash inversion operation to be non-destructive:
my %inverse; %inverse.push( %original.invert );
As pairs with already-seen keys get pushed onto the
%inverse hash, the original value isn’t overriden but instead "promoted" to an array. Here’s a small demonstration:
my %h; %h.push('foo' => 1); # foo => 1 %h.push('foo' => 2); # foo => [1, 2] %h.push('foo' => 3); # foo => [1, 2, 3]
All three (flip/reverse/invert) coerce their arguments to the expected type (if possible). For example if you pass a list to
flip, it is coerced into a string and then the string is rever^W flipped.