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.
=title How does interpolation of scalars, arrays and hashes work in Perl 6 =timestamp 2012-07-26T09:52:02 =author szabgab =keywords scalar, array, hash =status show =archive 1 =comments 1

=abstract start

When writing a large application that needs to generate all kinds of reports and page, you would probably want to separate

When building up strings from various pieces of data you often need to concatenate values.

Without interpolation this would mean lots of ~ signs and probably lots of quotations.

Luckily Perl 6 has a much better way for this. Perl 6 allows you to embed variables into strings, and when the string gets evaluated, it interpolates the value of your variable in the string.

People coming from Shell, Perl or Ruby might be familiar with some of this.

Or maybe they are not.

=abstract end

Scalar, Array and Hash interpolation

If you place a scalar variable in a double quoted string Perl 6 will put the value of the scalar in the the string just as it happens in Perl 5.

use v6;

my $name = "Foo";
say "Hello $name, how are you?";

And the output will look like this:

Hello Foo, how are you?

Arrays and Hashes are not interpolated in the same way, but there is a much more extended way for interpolating such variables.

Within a string anything you put in curly braces will be interpolated so if you have an array you can write:

use v6;

my @names = <Foo Bar Moo>;
say "Hello {@names}, how are you?";

to get this output:

Hello Foo Bar Moo, how are you?

The problem here is that based on that output you don't know if there were 3 elements in that array or two: "Foo Bar" and "Moo", or just one: "Foo Bar Moo".

Interpolating expressions

That's not a real problem.

The curly braces in the double quoted string allow you to put any expression between them. The expression will be evaluated and the result will be interpolated:

You can write:

use v6;

my @names = <Foo Bar Moo>;
say "Hello { join(', ', @names) } how are you?";

And the output will look like:

Hello Foo, Bar, Moo how are you?

While this still does not exactly show how many values are there, it provide a much bigger flexibility.

As a side note, in case you prefer object oriented code. You could write the above code as follows:

say "Hello { @names.join(', ') } how are you?";

Which will yield the same output as above.

Debugging prints

For ultimate debugging purposes, the best way to print is of course using the .perl method of the array:

say "Names: { @names.perl }";

That will print

Names:"Foo", "Bar", "Moo")

What if you'd also like to see the name of your variable as well? Then you can rely on the fact that arrays are not normally interpolated in strings and write:

say "@names = { @names.perl }";

That will print

@names ="Foo", "Bar", "Moo")

Just expressions

use v6;

say "Take 1+4";

will print

Take 1+4

as expected.

As I wrote you can put any expression in the curly braces, so you can also write

use v6;

say "Take {1+4}";

that will print

Take 5

I wonder if I could turn it into something to print Take Five?

Interpolating Hashes

There is nothing special in interpolating hashes either. It works just the same as

use v6;

my %phone = (
    foo => 1,
    bar => 2,

say "%phone = { %phone } ";

will print

%phone = foo   1 bar  2

Just as in the case of the arrays, I don't think this is very useful as it is unclear which are the keys and which are the values.

For debugging purposes, you'd better use the .perl method and write:

say "%phone = { %phone.perl } ";

which will print:

%phone = ("foo" => 1, "bar" => 2).hash

that's already a usable representation of the data.

Interpolating multidimensional arrays

Just to complete the picture I'd like to show a two dimensional array and how it looks when printed with the .perl method. Interpolated in a string.

use v6;

my @matrix = (
    [1, 2],
    [3, 4],

say "@matrix = { @matrix.perl }";


@matrix =[1, 2], [3, 4])

I think it makes it very clear what is in the array.

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:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.


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.91497118

Perl 6 Tricks and Treats newsletter

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