๐ŸŽ„ 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:


Run it a few times:


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:


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


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.

2 thoughts on “๐ŸŽ„ 1/25. Generating random passwords in Perl 6”

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