Skip to content

Commit

Permalink
Edited basics chapter, leaving one author note.
Browse files Browse the repository at this point in the history
  • Loading branch information
chromatic committed Jun 16, 2010
1 parent 11a5774 commit 2535b58
Showing 1 changed file with 58 additions and 53 deletions.
111 changes: 58 additions & 53 deletions src/basics.pod
Expand Up @@ -98,36 +98,42 @@ X<identifier>

A variable name begins with a I<sigil>, which is a non-word character such as
C<$>, C<@>, C<%>, or C<&>--or occasionally the double colon C<::>. The sigils
usually restrict the variable to some particular type. After the sigil comes an
I<identifier>, which may consist of letters, digits and the underscore. Between
letters you can also use a dash C<-> or an apostrophe C<'>, so C<isn't> and
C<double-click> are valid identifiers.
usually restrict the variable to a particular type, such as a single value or a
compound value. After the sigil comes an I<identifier>, which may consist of
letters, digits and the underscore. Between letters you can also use a dash
C<-> or an apostrophe C<'>, so C<isn't> and C<double-click> are valid
identifiers.

X<variable; scalar>
X<scalar>

The C<$> sigil indicates a I<scalar> variable, which indicates
that the variable stores a single value N<However, this single value
can still be a compound object like an array or a hash. When you iterate over
a C<$>-sigiled variable, it is interpreted as a single value, even if it
stores an array>.
=for author

The footnote is confusing.

=end for

The C<$> sigil indicates a I<scalar> variable, which indicates that the
variable stores a single valueN<However, this single value can still be a
compound object like an array or a hash. When you iterate over a C<$>-sigiled
variable, it is interpreted as a single value, even if it stores an array>.

X<files; handle>
X<file handle>
X<assignment>

The built-in function C<open> opens a file, here named F<scores>, and returns
an object representing that file, a I<file handle>. The equality sign C<=>
The built-in function C<open> opens a file, here named F<scores>, and returns a
I<file handle>--an object representing that file. The equality sign C<=>
I<assigns> that file handle to the variable on the left, which means that
C<$file> now stores the file handle.

X<strings; literal>
X<string literal>
X<string>

C<'scores'> is a I<string literal>. A string is a piece of text, a sequence of
characters. In this line, it's the argument provided to C<open>. If you prefer
C-style notation, you can write instead C<open('scores')>.
C<'scores'> is a I<string literal>. A string is a piece of text, and a string
literal is a string which appears directly in the program. In this line, it's
the argument provided to C<open>.

=begin programlisting

Expand All @@ -139,8 +145,8 @@ X<array>
X<method>
X<invocant>

The right-hand side calls a I<method>--a group of behaviors--named C<get> on
the file handle stored in C<$file>. This method reads and returns one line
The right-hand side calls a I<method>--a named group of behavior--named C<get>
on the file handle stored in C<$file>. This method reads and returns one line
from the file, removing the line ending. C<split> is also a method, called on
the string returned from C<get>. C<split>'s single argument is a string
containing a space character. C<split> decomposes its I<invocant>--the string
Expand All @@ -165,8 +171,8 @@ value that corresponds to a certain C<$key> with C<%hash{$key}>.N<Perl 5
programmers will notice that in Perl 6 the sigil sticks to variable, and does
not change upon indexing with C<[ ]> or C<{ }>.>

In the score counting program, C<%matches> stores the number of matches each player
has won. C<%sets> stores the number of sets each player has won.
In the score counting program, C<%matches> stores the number of matches each
player has won. C<%sets> stores the number of sets each player has won.

C<Array>s and C<Hash>es are central enough to each have their own sigil in
Perl. The C<$> sigil, however, confers no restriction on the type of the value
Expand Down Expand Up @@ -312,9 +318,9 @@ X<autovivification>

Before these two lines execute, C<%sets> is empty. Adding to an entry not in
the hash will cause that entry to spring into existence just-in-time, with a
value starting at zero. (This is called I<autovivification>). After these two
lines have run for the first time, C<%sets> contains C<< 'Ana' => 3, 'Dave' =>
0 >>. (The fat arrow C<< => >> separates key and value in a C<Pair>.)
value starting at zero. (This is I<autovivification>). After these two lines
have run for the first time, C<%sets> contains C<< 'Ana' => 3, 'Dave' => 0 >>.
(The fat arrow C<< => >> separates key and value in a C<Pair>.)

=begin programlisting

Expand All @@ -326,9 +332,9 @@ lines have run for the first time, C<%sets> contains C<< 'Ana' => 3, 'Dave' =>

=end programlisting

If C<$r1> has a larger value than C<$r2>, C<%matches{$p1}> increments by one. If
C<$r1> is not larger than C<$r2>, C<%matches{$p2}> increments. Just as in the
case of C<+=>, if either hash value did not exist previously, it is
If C<$r1> has a larger value than C<$r2>, C<%matches{$p1}> increments by one.
If C<$r1> is not larger than C<$r2>, C<%matches{$p2}> increments. Just as in
the case of C<+=>, if either hash value did not exist previously, it is
autovivified by the increment operation.

X<postincrement>
Expand All @@ -337,10 +343,10 @@ X<preincrement>
X<operators; preincrement>

C<$thing++> is short for C<$thing += 1> or C<$thing = $thing + 1>, with the
small exception that the return value of the expression is C<$thing>, not the
incremented value. If, as you can do in many other programming languages, you
can use C<++> as a prefix, it returns the incremented value; C<my $x = 1; say
++$x> prints C<2>.
small exception that the return value of the expression is C<$thing> I<before>
the increment, not the incremented value. If, as you can do in many other
programming languages, you can use C<++> as a prefix, it returns the
incremented value; C<my $x = 1; say ++$x> prints C<2>.

=begin programlisting

Expand All @@ -352,45 +358,44 @@ X<topic variable>
X<topic>
X<variables; $_>

This might look a bit scary at first, but it consists of three individually
simple steps. An array's C<sort> method returns a sorted version of the array's
contents. However, the default sort on an array sorts by its contents. To
print the players names in winner-first order, the code must sort the array by
the I<scores> of the players, not their names. The C<sort> method can take an
argument, a I<block> used to transform the array elements (the names of
players) to the data by which you wish to sort. The array items are passed in
through the I<topic variable> C<$_>.
This line consists of three individually simple steps. An array's C<sort>
method returns a sorted version of the array's contents. However, the default
sort on an array sorts by its contents. To print player names in winner-first
order, the code must sort the array by the I<scores> of the players, not their
names. The C<sort> method's argument is a I<block> used to transform the array
elements (the names of players) to the data by which to sort. The array items
are passed in through the I<topic variable> C<$_>.

X<block>

You have seen blocks before: both the C<for> loop C<< -> $line { ... } >> and
the C<if> statement worked on blocks. A block is a self-contained piece of
Perl 6 code, optionally with a signature (the C<< -> $line >> part). See
Perl 6 code with an optional signature (the C<< -> $line >> part). See
L<signatures> for more information.

The simplest way to sort the players by score would be C<@names.sort({
%matches{$_} })>, which sorts by number of matches won. However Ana and Dave have
both won two matches. That simple sort doesn't account for the number of sets
won, which is the secondary criterion to decide who has won the tournament.
%matches{$_} })>, which sorts by number of matches won. However Ana and Dave
have both won two matches. That simple sort doesn't account for the number of
sets won, which is the secondary criterion to decide who has won the
tournament.

X<stable sort>
X<sort; stable>

When two array items have the same value, C<sort> leaves them in the same order
as it found them. Computer scientists call this a I<stable sort>. The program
as it found them. Computer scientists call this a I<stable sort>. The program
takes advantage of this property of Perl 6's C<sort> to achieve the goal by
sorting twice: first by the number of sets won (the secondary criterion), then
by the number of matches won.

After the first sorting step, the names are in the order C<Beth Charlie Dave
Ana>. After the second sorting step, it's still the same, because nobody has
won fewer matches but more sets than somebody else. Such a situation is
entirely possible, especially at larger tournaments.
Ana>. After the second sorting step, it's still the same, because no one has
won fewer matches but more sets than someone else. Such a situation is entirely
possible, especially at larger tournaments.

C<sort> sorts in ascending order, from smallest to largest. This is the
opposite of the desired order. Therefore, the code calls the C<.reverse>
method on the result of the second sort, and stores the final list in
C<@sorted>.
opposite of the desired order. Therefore, the code calls the C<.reverse> method
on the result of the second sort, and stores the final list in C<@sorted>.

=begin programlisting

Expand All @@ -415,10 +420,10 @@ newline at the end.)
X<interpolation>

When you run the program, you'll see that C<say> doesn't print the contents of
that string verbatim. For example, in place of C<$n> it prints the names of
players stored in C<$n>--the contents of C<$n>. This automatic substitution is
I<interpolation>. Perl 6 quotes can interpolate variables with the dollar
sigil as well as blocks of code in curly braces.
that string verbatim. In place of C<$n> it prints the contents of C<$n>-- the
names of players stored in C<$n>. This automatic substitution of code with its
contents is I<interpolation>. Perl 6 quotes can interpolate variables with the
dollar sigil as well as blocks of code in curly braces.

X<double-quoted strings>
X<strings; double-quoted>
Expand All @@ -445,8 +450,8 @@ right

B<1.> The input format of the example program is redundant: the first line
containing the name of all players is not necessary, because you can find out
which players participated in the tournament simply by looking at their names
in the subsequent rows.
which players participated in the tournament by looking at their names in the
subsequent rows.

How can you change the program if the first input line is omitted?
Hint: C<%hash.keys> returns a list of all keys stored in C<%hash>.
Expand Down

0 comments on commit 2535b58

Please sign in to comment.