πŸ“˜ Generating random passwords using Perl 6

Generate a random string that can be used as a password.

One of the possible solutions looks like this:

say ('0' .. 'z').pick(15).join('');

The pickmethod with no arguments takes a random element from the range of the ASCII charactersbetween 0and z. In the above example, calling pick(15) selects 15 different characters, which are then joined together using the join method.

It is important to know the two key features of the pick method. First, when called with an integer argument bigger than 1, the result contains only unique elements. Thus, all the characters in the password are different.

The second feature is a consequence of the first one. If the provided list of elements is not long enough, and its length is less than the argument of pick, the result is as long as the original data list.

To see which elements are used for generating a password, run the code with a number bigger than the whole ASCII sequence:

say ('0' .. 'z').pick(1000).sort.join('');

With this request, you will see all the characters that participate in forming a random password:

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`
abcdefghijklmnopqrstuvwxyz
An example of a generated password: 05<EV]^bdfhnpyz.

To limit the character range, list the characters you want to see in a password:

my @chars = '0' ... '9', 'A' ... 'Z', 'a' ... 'z';
say @chars.pick(15).join('');

Now the password only contains alphanumeric symbols: 2zOIySp5PHb08Ql.

The ... operator creates a sequence. Be warned not to use the .. operator, which creates a range, in which case the @chars array is an array of ranges rather than a flat array of single characters.

The solution is quite elegant and does not require explicit use of the randfunction. Neither loops are needed.

Now, let us solve the task in such a way that characters may be repeated in the result. It is still possible to use the pickmethod but it should be called independently a few times.

my $password = '';
$password ~= ('0' .. 'z').pick() for 1..15;
say $password;

Now, the password can include the same character more than once, for example: jn@09Icoys@tD;o.

Finally, let us stick to the intermediate solution, where the password is formed via a few groups of random strings, and each such substring contains only unique characters:

my $password = '';
$password ~= ('0' .. 'z').pick(5).join('') for 1..3;
say $password;

As the pick returns a sequence, the join method is needed to concatenate characters to a string with no gaps.

πŸ“˜ Incrementing filenames using Perl 6

Generate a list of filenames like file1.txt, file2.txt, etc.

Perl 6 allows incrementing those kinds of filenames directly:

my $filename = 'file0.txt';
for 1..5 {
   $filename++;
    say $filename;
}

This program prints the list of consequent filenames:

file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

Notice that after reaching 9, the letter from file is incremented. Thus, file9.txt is followed by filf0.txt. To prevent that, add enough zeros in the template:

my $filename = 'file000.txt';
for 1..500 {
   $filename++;
    say $filename;
}

Now, the sequence starts with file001.txt and continues to file500.txt. Multiple file extensions in the template, say file000.tar.gz, are also handled properly, so the numeric part is incremented.

πŸ“˜ Camel case using Perl 6

Create a camel-case identifier from a given phrase.

It is a good practice to follow some pattern when choosing names for variables, functions, and classes in any programming language. In Perl 6, identifiers are case-sensitive, and, unlike many other languages, hyphens are allowed. So, variables names like $max-span or function names like celsius-to-fahrenheit are accepted.

In this task, we will form the CamelCase variable names from a given phrase. Names created in this style are built of several words; each of which starts with a capital letter.

Here’s the program that does the required conversions:

my $text = prompt('Enter short text > ');
my $CamelName = $text.comb(/\w+/).map({.lc.tc}).join('');
say $CamelName;

All the actions are done in a sequence of method calls. The words are selected from the input $text using the comb method with a regex /\w+/. Then, each found word is mapped using another chained method call: 

{ .lc.tc }

A β€˜bare’ dot means that the method is called on the default variable $_, which is repeatedly set to the current element. In Perl 6, there is no ucfirst method to make the first letter of the text uppercase. Instead, we use the tc method (tc stands for Title Case), and call it on the result of the lc call to make sure that after calling .lc.tc, all the letters are lowercase except the first one. Finally, the elements of the array are joined together with the help of the join method. The input string β€˜Hello, World!’ becomes HelloWord after all the transformations are done.

πŸ“˜ Removing blanks from a string using Perl 6

Remove leading, trailing and double spaces from a given string.

This task often occurs when you need to clean the user input, such as from web forms, where leading or trailing spaces in, for example, names, are most likely user mistakes and should be removed.

Removing double and multiple spaces between words can be solved by using substitution:

my $string = 'Hello,     World!';
$string ~~ s:g/\s+/ /;

Don’t forget to make the substitution global with the :g adverb to find all the occurrences of repeated spaces.

Leading and trailing spaces can be removed with the trimroutine:

my $string = 'Hello, World! ';
say trim($string);

The trim routine exists as a self-standing function, as shown in the previous example, as well as a method of the Str class, so it can be called on a variable or on a string:

say $string.trim;
say ' Hello, World! '.trim;

There are also two routines, trim-leading and trim-trailing, which remove either only leading or only trailing spaces.

say 'Β‘' ~ '  Hi '.trim-leading;  # Β‘Hi␣␣
say '  Hi  '.trim-trailing ~ '!'; # β£β£Hi!

πŸ“˜ Reverse a string using Perl 6

Print a string in the reversed order from right to left.

Strings, or the objects of the Str class, have the flip method, which does the work:

my $string = 'Hello, World!';
say $string.flip;

This code prints the desired result:

!dlroW ,olleH

TheΒ flip routine may be called both as a method on a string and as a stand-alone function:

say flip 'Abcdef'; # fedcbA
say 'Word'.flip;   # droW

Don’t forget thatΒ say can also be called as a method:

'Magic'.flip.say; # cigaM

In Perl 6, there is also theΒ reverse routine, but it cannot be applied directly to strings. It accepts lists, so a string first has to be converted to the list of characters, then reversed, and later joined again to a string.

Here is the code that works according to this description.

my $string = 'Hello, World!';
my $reversed = $string.split('').reverse().join('');
say $reversed; # !dlroW ,olleH

πŸ“˜ 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:

$number.Str.comb

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:

$number.comb.unique

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;