πŸ“˜ Str type in Perl 6

πŸ“˜ Str type in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

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'); # 4

Let 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; # 6

Getting 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);

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code