๐Ÿ“˜ Finding unique digits using Perl 6

Print unique digits from a given integer number.

The task is easily solved if an integer is immediately converted to a string.

my $number = prompt('Enter number> ');
say $number.comb.unique.sort.join(', ');

The combmethod, called with no arguments, splits the string into separate characters. The method is defined in the Str class; thus, the $number is converted to the string first. The same may be explicitly written as:


Notice that in the case of using $number.split(''), empty elements are added to the beginning and the end of the array.

At this moment, an initial number resides in an array, each element of which is a digit from the $number.

Taking unique elements of an array does not require any manual programming in Perl 6, as the Array class contains a special method for that:


Finally, to make the result look better, the array of unique digits is sorted and printed as a comma-separated list:

$number.comb.unique.sort.join(', ')

Instead of calling sayas a stand-alone function, it may be called as a method on the resulting string:

$number.comb.unique.sort.join(', ').say;

๐Ÿ“˜ String length in Perl 6

Print the length of a string.

Perl 6 handles all strings as UTF-8 by default. This is why there is more than one parameter describing the length of the string. In fact, the lengthroutine does not exist, and an attempt to use it issues an error message with some hints to which other methods you can use.

To get the length of the string in the sense of number of characters, use the charsmethod:

say 'hello'.chars;  # 5
say 'cafรฉ'.chars;   # 4
say 'ะฟั€ะธะฒะตั‚'.chars; # 6

The results reflect the intuitive expectation and do not depend on actual representation of the characters. The first string fits in the ASCII table, the second may still be encoded in an 8-bit encoding Latin-1, and the third needs two bytes per character in the UTF-8 encoding. 

Another method, codes, returns the number of codepoints in the Unicode space. For the above examples, both charsand codesreturn the same numbers, as all the characters can be represented by a single codepoint.

say 'hello'.codes;  # 5
say 'cafรฉ'.codes;   # 4
say 'ะฟั€ะธะฒะตั‚'.codes; # 6

Although, when using combining characters, you may create a character that does not exist as a separate character in the Unicode table. In this case, the results of charsand codesmay differ.

Consider an example with a character built out of two elements: Latin letter xand a combining character COMBINING OGONEK. Together, they form a non-existing letter, which is one character, but two codepoints:

say 'x'ยญยญ.chars; # 1
say 'x'.codes; # 2

Let us dig a bit into how the above character is represented in the UTF-8 encoding. It consists of two parts: LATIN SMALL LETTER X and the combining character COMBINING OGONEK. The letter itself is a one-byte code 0x78, and the combining character has the Unicode entry point 0x328 and needs two bytes in UTF-8: 0xCC 0xA8.

Let us rewrite the example by explicitly specifying the codepoint of the combining character:

say "x\x[0328]".chars; # 1
say "x\x[0328]".codes; # 2

The above example was about the character that does not exist in Unicode as a single codepoint. Now, let us use another letter, saye, which forms an existing character with the same combining character: ฤ™.

say 'ฤ™'.chars; # 1
say 'ฤ™'.codes; # 1

In this case, both charsand codesmethods return 1. Even if the string is built using an explicit combining character, the codesmethod coerces it back to the proper codepoint and does not count it as a separate code:

say "e\x[0328]".chars; # 1
say "e\x[0328]".codes; # 1

Thus, in many cases, to get the length of a string, it is enough to use the charsmethod called on that string.

๐Ÿ“˜ Greet a person using Perl 6

Ask a user for their name and greet them by printing โ€˜Hello, <Name>!โ€™

Perl 6 offers a simple promptfunction that performs both actions: prints a prompt and reads the input. So, the program using it may look like this:

say 'Hello, ' ~ prompt('Enter your name: ') ~ '!';

The ~operator stands for string concatenation in Perl 6. Donโ€™t be confused by the sequence of text strings in this code. To build the string, Perl needs to have all its parts. Two of them ('Hello',and '!') are presented by literal strings, while the middle part needs user input. Therefore, the flow of the whole program remains logical:

Enter your name: Andy
Hello, Andy!

If you prefer a more traditional program flow, split it into separate parts and interpolate a variable in a string:

my $name = prompt('Enter your name: ');
say "Hello, $name!";

Alternatively, the get function may be used. It returns the input line without the newline character. Printing a prompt message is your responsibility now:

print 'Enter your name: ';
my $name = get();
say "Hello, $name!";

The get function may be called as a method on the $*IN variable, which is by default connected to the standard input:

my $name = $*IN.get();

๐Ÿ“˜ Hello, World! in Perl 6

Print โ€˜Hello, World!โ€™

There are two built-in functions in Perl 6 to print to the console: printand say. Both print their arguments, but the say routine additionally ends the output with a newline character.

So, the quickest solution is to use sayand pass a string with no newlines:

say 'Hello, World!'

Another solution is to use print and include the \n character in the string itself:

print "Hello, World!\n"

The output of either program is the same:

Hello, World!

Notice the difference between single and double quotes: single quotes do not interpolate special characters like \n while the double quotes do. Thereโ€™s no mistake in using double quotes for strings without special characters, while it is better to use the appropriate quoting style when you do not expect variables in the string and when there is no need to interpolate variables.Another thing to take a look at in the examples above is that a semicolon is not required for one-line programs.