Camelia

Introduction to Perl 6 using REPL: scalars

This screencast was first published in July 2010, but the examples are mostly still correct.

Note! This site is about Perl 6.
If you are looking for a solution for the current production version of Perl 5, please check out the Perl 5 tutorial.

See the notes for some changes in Perl 6 and mistakes I made during the recording.

Rakudo

IRC: #perl6 on irc.freenode.net

REPL

$ perl6

> say 42
42

> say "hello"
hello

> "hello".say
hello

> slurp '01.p6'

examples/01.p6

use v6;

say "Hello world";

> EVAL slurp '01.p6'
Hello world

Instead of eval Perl 6 has EVAL now.

> slurp '02.p6'

examples/02.p6

use v6;

my $name = prompt "Please type in your name: ";

say "Hello $name, how are you?";

Scalars - variables starting with a $ sign - can actually contain any value, including arrays and hashes, but that will be discussed later.

Variables need to be declared, except in the REPL which is used in the screencast.

Variable interpolation!

> EVAL slurp '02.p6'
Please type in your name: Foo
Hello Foo, how are you?
>

prompt is automatically removing the newline from the end of the input. It is chomp-ing the input.

> EVAL slurp '03.p6'
When were you born? 2000
You are younger than Perl by 13 years

> slurp '03.p6'

examples/03.p6

use v6;

my $year = prompt "When were you born? ";
if $year > 1987 {
    say "You are younger than Perl by { $year - 1987 } years";
}


No need for parentheses around the condition of the if statement.

In Perl 6 string-interpolation of any expression is possible by putting the expression inside curly braces within the double-quoted string. The result of the expression will be embedded in the string.

> slurp '04.p6'

examples/04.p6

use v6;

my $year = prompt "When were you born? ";
if 1995 > $year > 1987 {
    say "You are younger than Perl 1 but older than Perl 5";
}

Chained conditionals.

> slurp '05.p6'

examples/05.p6

use v6;

my $luck = prompt "What is your lucky number? ";
if $luck == 3 or $luck == 7 or $luck == 13 {
    say "Oh, that's like mine";
}

if $luck == 3|7|13 {
    say "Oh, that's still like mine";
}

3|7|13 - a junction.


The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Email:
Full name:
This is a newsletter temporarily running on my personal site (szabgab.com) using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2015-01-17



Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 3.79252035

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: