๐Ÿ“˜ 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.

๐Ÿ“˜ How to debug Perl 6 programs

For quick tests, use the compiler in the mode of the REPL (readโ€”evalโ€”print loop) shell. Just run theย perl6 command:

$ perl6
To exit type 'exit' or '^D'

With bigger programs, one of the following techniques helps to visualise data:

1. Theย say routine is used as a stand-alone function or as an object method. It works well with both scalar and aggregate data, such as arrays, hashes, or objects:

say $x;

2. Theย perl method, which returns the representation of an object in the Perlย 6 syntax:

say {a => 1, b => 2}.perl; # {:a(1), :b(2)}

3. The WHAT and the ^name methods, which give you the information about the object type or class name:

my Int $x;
say $x.WHAT;  # (Int)
say $x.^name; # Int

4. Theย dd routine. This is a Rakudo-specific feature that dumps an object:

my @a = 1..5;
dd @a; # Array @a = [1, 2, 3, 4, 5]