Camelia

Perl 6 Interactive Shell - values in $ variables

When learning Perl 6, one of the best things you can do is to experiment with the language in the interactive shell. (Also known as REPL = Read Eval Print Loop)

Once you have Rakudo Perl 6 installed, open a Terminal (or Command Prompt in MS Windows) and type in perl6. After a second or so, you will see:

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

To exit type 'exit' or '^D'
>

(TBD: Is this the same on Windows?)

The > sign is the prompt. You can now type in any Perl 6 expression. Let's start with a few simple ones:

> say "hello world"
hello world
> say 42
42
> 19 + 23
42

say is the keyword to print something to the screen and add a trailing newline.

Strings are in double-quotes. Numbers don't have quotes.

We can use Perl 6 as a calculator. In the interactive shell we don't even need to call say as it will automatically print the result of the last statement. (The P is in the name REPL for a reason.)

Variables

Most variables in Perl 6 have one or two special characters at the beginning. The first such character is called Sigil, the second, optional character is called Twigil.

The $ sigil means the variable can contain any item. Let's try to assign a number to a variable that starts with a $:

> $x = 42
===SORRY!=== Error while compiling:
Variable '$x' is not declared
------> <BOL>⏏$x = 42

Even the interactive shell requires us to declare our variables using the my keyword:

> my $x = 42
42
> $x
42
> $x - 7
35

After the declaration and the assignment, we can use the variable in arithmetic operations.

Using the ^name method we can also ask the variable what type is it. In this case it is of type Int:

> $x.^name
Int

We can replace the content of the variable with a string, and then, when we ask what is the ^name of its type, we get back that it is a Str.

> $x = "abc"
abc

> $x.^name
Str

Some people love this kind of flexibility, for others this looks crazy dangerous. Don't worry, In Perl , you can be much stricter with your variable types during declaration. If that's your thing:

> my Int $w = 42
42
> $w.^name
Int

> $w = "hello"
Type check failed in assignment to $w; expected Int but got Str ("hello")
  in block <unit> at <unknown file> line 1

>

In this case, we've declared the $w variable to be Int so we won't be able to assign any other type to it.

Back to our type declaration free variable, you can put all kind of other types in that variable:

> $x = 3.14
3.14
> $x.^name
Rat

> $x = ["foo", "bar", "qux"]
[foo bar qux]
> $x.^name
Array

> $x = ("foo", "bar", "qux")
(foo bar qux)
> $x.^name
List

> $x = {name => "Foo", answer => 42}
{answer => 42, name => Foo}
> $x.^name
Hash

> $x = name => "Foo"
name => Foo
> $x.^name
Pair

We'll look at these types later on.

Pair

> $x = name => "Foo"
name => Foo
> $x.^name
Pair
> $x.gist
name => Foo
> $x.key
name
> $x.value
Foo

Hash

A Hash is a key-value store. In other languages it might be called Associative Array or Dictionary.

We can create a hash using curly-braces, and assign it to a variable starting with a $:

> my $x = {name => "Foo", answer => 42};
{answer => 42, name => Foo}

It is a hash:

> $x.^name
Hash

We can access the values of the individual keys both with quotes around the keys:

> $x{"name"}
Foo
> $x{"answer"}
42

And without any quotes:

> $x<name>
Foo
> $x<answer>
42

We can fetch the keys of the hash and the values separately:

> $x.keys
(name answer)

> $x.values
(Foo 42)

What keys really returns is a Seq (Sequence) object:

> my $z = $x.keys
(name answer)
> $z.^name
Seq

We can also iterate over the individual keys using the for loop. ($k is the loop variable that will get each key on its turn.)

> for $x.keys -> $k { say $k }
name
answer

Using the keys we can access the respective values:

> for $x.keys -> $k { say $k; say $x{$k} }
name
Foo
answer
42

Alternatively we can fetch a list of pairs and on each Pair object we can call the key and the value methods:

> for $x.pairs -> $p { say $p.^name; say $p.key; say $p.value}
Pair
name
Foo
Pair
answer
42

List

If we wrap the right-hand side in parentheses, we get a List on the left-hand side:

> $x = ("foo", "bar", "qux")
(foo bar qux)
> $x.^name
List

In order to iterate over its elements, we need to call the list method on it:

> for $x.list -> $v { say $v }
foo
bar
qux

Array

If we put square brackets around the values on the right-hand side, we get an Array

> $x = ["foo", "bar", "qux"]
[foo bar qux]
> $x.^name
Array

Here too we need to call the list method in order to iterate over the elements:

> for $x.list -> $i { say $i }
foo
bar
qux


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 2017-05-26



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: 2.4188513

Perl 6 Tricks and Treats newsletter

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