In Perl 5, starting with version 5.8, Unicode support is fairly well implemented - but people still complain about the difficulty of using it. Mainly due to the fact that the programmer needs to keep track of which lines have been decoded and which should be processed as binary data. And there is no reliable way to look at the variables and understand whether the binary is a string or text.
In Perl6, this problem was solved by entering separate types. Str stores text. Perl6 string literals are of type Str. Binary data is stored in Buf objects. And they can not be confused. Conversion between them is carried out using the methods encode and decode.
my $buf = Buf.new(0x6d, 0xc3, 0xb8, 0xc3, 0xbe, 0x0a); $*OUT.write($buf); my $str = $buf.decode('UTF-8'); print $str;
')
In both operations, the effect is the same - they output the standard output stream "mø " and the line feed. Buf.new (...) takes a list of integers from 0 to 255 - the bytes from which the new buffer is built. $ * OUT.write ($ buf) outputs the buffer from $ buf to the standard output stream.
$ buf.decode ('UTF-8') decodes the buffer and returns a Str object (or falls if the buffer does not contain a valid string in UTF-8). The inverse operation is $ Buf.encode ($ encoding). Str can be output simply through print.
Naturally, print must also convert the string to a binary representation in the process. For this (and other similar operations), the default encoding is UTF-8. The Perl6 specification states that the user can change the default settings (but so far the compilers do not support this).
For reading, you can use the .read ($ no-of-bytes) methods (and you get a Buf) or .get (and you get a Str). The read and write methods are present not only in files and streams, but also in sockets.
In Perl 5, you can make a very unpleasant mistake by using concatenation or in some other way (join, text interpolation) by combining text and binary strings. The result is a “broken” string — but only when it contains bytes above 127. In such situations, it is extremely difficult to debug the code.
In Perl6, in this case, you just get the error "Cannot use a Buf as a string".