Skip to content

Commit

Permalink
[basics] be more verbose about interpolation of arrays and hashes; mo…
Browse files Browse the repository at this point in the history
…re examples
  • Loading branch information
moritz committed Aug 10, 2010
1 parent b47f718 commit 7875129
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/basics.pod
Expand Up @@ -442,33 +442,58 @@ X<strings; single-quoted>
Double quoted strings in Perl 6 can interpolate variables with the C<$>
sigil as well as blocks of code in curly braces. Since any arbitrary
Perl code can appear within curly braces, C<Array>s and C<Hash>es may be
interpolated by placing them within curly bracesN<There is another
interpolation method known as the I<M<zen slice>>. If an array name is
followed by empty subscripts within the double quoted string (i.e. C<say
"@array[]";>), then the array is interpolated as a space separated list.
Similarly, if a hash is followed by empty subscripts (i.e. C<say
"%h{}">) then the C<Hash> is interpolated as a space separated list of
values. The keys of the C<Hash> are not interpolated.>. C<Arrays> within
interpolated by placing them within curly braces.


Arrays within
curly braces are interpolated with a single space character between each
item. C<Hash>es within curly braces are interpolated as a series of
item. Hashes within curly braces are interpolated as a series of
lines. Each line will contain a key, followed by a tab character, then
the value associated with that key, and finally a newline.

=begin programlisting

say "Math: { 1 + 2 }" # Math: 3
my @people = <Luke Matthew Mark>;
say "The synoptics are: {@people}" # The synoptics are: Luke Matthew Mark
say "Math: { 1 + 2 }" # Math: 3
my @people = <Luke Matthew Mark>;
say "The synoptics are: {@people}" # The synoptics are: Luke Matthew Mark

say "{%sets}"; # From the table tennis tournament

# Charlie 4
# Dave 6
# Ana 8
# Beth 4

=end programlisting


When array and hash variables appear directly in a double-quoted string (and
not inside curly brackets), they are only interpolated if their name is
followed by a postcircumfix -- a bracketing pair that follows a
statement. It's also ok to have a method call between the variable name and
the postcircumfix.

X<Zen slice>

=begin programlisting

my @flavours = <vanilla peach>;

say "we have @flavours"; # we have @flavours
say "we have @flavours[0]"; # we have vanilla
# so-called "Zen slice"
say "we have @flavours[]"; # we have vanilla peach

say "{%sets}"; # From the tennis tournament
# method calls ending in postcircumfix
say "we have @flavours.sort()"; # we have peach vanilla

# Charlie 4
# Dave 6
# Ana 8
# Beth 4
# chained method calls:
say "we have @flavours.sort.join(', ')";
# we have peach, vanilla

=end programlisting


=head1 Exercises

B<1.> The input format of the example program is redundant: the first line
Expand Down

0 comments on commit 7875129

Please sign in to comment.