🎄 1/25. Generating random passwords in Perl 6

Welcome to the first day of the brand new Perl6.Online Advent Calendar! This year, its theme is Perl 6 One-Liners (a pun from perl6.onliners). Thus, welcome to this year’s Perl 6 One-Liner Advent Calendar. The whole perl6.online blog was initially planned to be daily, so it’s a great opportunity to keep the pace for at least another 25 days.

Don’t forget to follow other Perl-related calendars written by other people:

In the following days, I will be publishing short articles that either explain the code from the Perl 6 Calendar 2019 or solve some problems inspired by Project Euler, where Perl 6 can demonstrate its extreme beauty (if you don’t want to see my solutions prior to yours, I invite you to solve the tasks yourself first, and only then read the articles). But today, let’s start with something else and generate a random password in Perl 6.

Here’s the full code:

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

Run it a few times:

Z4x72B8wkWHo0QD
J;V?=CE84jIS^r9
2;m6>kdHRS04XEL
O6wK=umZ]DqyHT5
3SP\tNkX5Zh@1C4
PX6QC?KdWYyzNOc
bq1EfBZNdK9vHxz

Our line generates different strings each time you run it. Try it yourself to create a password or take one of the above-shown passwords if you did not install Perl 6 yet.

Some time ago, we looked into the internals of the pick method. It is defined in the Any class, and it actually is delegating work to the method of the List class:

proto method pick(|) is nodal {*}
multi method pick() { self.list.pick }
multi method pick($n) { self.list.pick($n) }

The join method is defined in the List class too. Let’s not examine the body of it now, as it is quite big, but let’s look at the signature:

method join(List:D: Str(Cool) $separator = '') is nodal

It is clearly seen that when invoking the method with no arguments, the default separator is an empty string, which is exactly what we need for passwords. (Learn more about the is nodal trait in the documentation; it is said there that the trait is not supposed to be widely used in common code.)

You may also ask a question: What characters can I expect in the password? Great that you ask! Perl 6 is a Unicode-oriented language, and the range '0'..'z' seems to contain characters with different Unicode properties (i. e., digits and letters at least). To see what’s inside, just remove the pick method from our today’s code:

('0'..'z').join.say

This line prints the subset of the ASCII table between the two given characters:

0123456789:;<=>?@
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`
abcdefghijklmnopqrstuvwxyz

These are the characters that may appear in the password. The pick method makes sure characters are not repeated.

And that’s all for today, see you tomorrow with another line of Perl 6 code.

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