📘 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.txtfile2.txtfile3.txtfile4.txtfile5.txt Notice that after reaching 9, the e letter from file is incremented. Thus, file9.txt is followed by filf0.txt. To prevent that, add enough zeros in … Continue reading “📘 Incrementing filenames using Perl 6”

📘 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 … Continue reading “📘 Camel case using Perl 6”

📘 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 … Continue reading “📘 Removing blanks from a string using Perl 6”

📘 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 … Continue reading “📘 Reverse a string using Perl 6”

📘 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 … Continue reading “📘 Finding unique digits using Perl 6”

📘 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 … Continue reading “📘 String length in Perl 6”

📘 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 … Continue reading “📘 Greet a person using Perl 6”

📘 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 … Continue reading “📘 Hello, World! in Perl 6”