Raku Books / Perl 6 at a Glance / Perl 6 Essentials / Built-in types
Str
Str is no doubt a string. In Perl 6, there are methods
to manipulate strings. Again, you call them as methods on objects.
my $str = "My string";
say $str.lc; # my string
say $str.uc; # MY STRING
say $str.index('t'); # 4Let us now get the length of a string. The naïve attempt to write
$str.length produces an error message. However, a hint is
also provided:
No such method 'length' for invocant of type 'Str'
Did you mean 'elems', 'chars', 'graphs' or 'codes'?Thus, we have a simple and a mono-semantic method to get the length of a Unicode string.
say "περλ 6".chars; # 6Getting used to the new way of working with strings as objects may
take some time. For example, this how you can call the
printf as a method on a string:
"Today is %02i %s %i\n".printf($day, $month, $year);