📘 Greet a person using Perl 6

📘 Greet a person using Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

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();

Leave a Reply

Your email address will not be published. Required fields are marked *