
Perl 6: Different Names for Different Things
- Transfer
Newcomers to Perl 5 complain that there is no line reversal tool in the language. There is a reverse function, but for some reason it does not work:
Having gained experience, they find a solution. The function works in two modes. In a list context, it reverses lists, and in a scalar context, lines:
Unfortunately, this is an exception to the Perl context model. Most operators and functions define the context, after which the data is interpreted in this context. + and * work with numbers. works with strings. Therefore, the symbol (or the name of a function of type uc) defines the context. In the case of reverse, this is not so.
In Perl 6, we took into account the mistakes of the past. Therefore, the reverse of lines, lists, and inverting hashes are separated:
When inverting hashes, the type of result is different from the type of input. Since the values of the hashes do not necessarily differ, inverting the hash into a hash can lead to data loss. Therefore, it is not a hash that is returned, but a list of pairs - and what to do with it, the programmer decides.
Here's how to do a non-destructive invert operation:
When pairs with existing keys are added to% inverse hash, the original value is not overwritten, but turns into an array.
Whenever possible, all three of these statements convert arguments to the type they expect. If you pass the list to flip, it will be turned into a string, and the string will be reversed.
$ perl -E "say reverse 'привет'"
привет
Having gained experience, they find a solution. The function works in two modes. In a list context, it reverses lists, and in a scalar context, lines:
$ perl -E "say scalar reverse 'привет'"
тевирп
Unfortunately, this is an exception to the Perl context model. Most operators and functions define the context, after which the data is interpreted in this context. + and * work with numbers. works with strings. Therefore, the symbol (or the name of a function of type uc) defines the context. In the case of reverse, this is not so.
In Perl 6, we took into account the mistakes of the past. Therefore, the reverse of lines, lists, and inverting hashes are separated:
# реверс строк – то бишь, переворот:
$ perl6 -e 'say flip "привет"'
тевирп
# реверс списков
# perl6 -e 'say join ", ", reverse '
ef, cd, ab
# инвертирование хэшей
perl6 -e 'my %capitals = Франция => "Париж", Англия => "Лондон";
say %capitals.invert.perl'
("Париж" => " Франция ", " Лондон " => " Англия ")
When inverting hashes, the type of result is different from the type of input. Since the values of the hashes do not necessarily differ, inverting the hash into a hash can lead to data loss. Therefore, it is not a hash that is returned, but a list of pairs - and what to do with it, the programmer decides.
Here's how to do a non-destructive invert operation:
my %inverse;
%inverse.push( %original.invert );
When pairs with existing keys are added to% inverse hash, the original value is not overwritten, but turns into an array.
my %h;
%h.push('foo' => 1); # foo => 1
%h.push('foo' => 2); # foo => [1, 2]
%h.push('foo' => 3); # foo => [1, 2, 3]
Whenever possible, all three of these statements convert arguments to the type they expect. If you pass the list to flip, it will be turned into a string, and the string will be reversed.