πŸ“˜ Str type in Perl 6

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s