From 2535b58c728547b295b2d10ff2f9a14aa01ba952 Mon Sep 17 00:00:00 2001 From: chromatic Date: Wed, 16 Jun 2010 14:09:41 -0700 Subject: [PATCH] Edited basics chapter, leaving one author note. --- src/basics.pod | 111 ++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/src/basics.pod b/src/basics.pod index 08d5b5d..6a32122 100644 --- a/src/basics.pod +++ b/src/basics.pod @@ -98,26 +98,32 @@ X A variable name begins with a I, 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, which may consist of letters, digits and the underscore. Between -letters you can also use a dash C<-> or an apostrophe C<'>, so C and -C 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, which may consist of +letters, digits and the underscore. Between letters you can also use a dash +C<-> or an apostrophe C<'>, so C and C are valid +identifiers. X X -The C<$> sigil indicates a I variable, which indicates -that the variable stores a single value N-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 variable, which indicates that the +variable stores a single valueN-sigiled +variable, it is interpreted as a single value, even if it stores an array>. X X X -The built-in function C opens a file, here named F, and returns -an object representing that file, a I. The equality sign C<=> +The built-in function C opens a file, here named F, and returns a +I--an object representing that file. The equality sign C<=> I that file handle to the variable on the left, which means that C<$file> now stores the file handle. @@ -125,9 +131,9 @@ X X X -C<'scores'> is a I. A string is a piece of text, a sequence of -characters. In this line, it's the argument provided to C. If you prefer -C-style notation, you can write instead C. +C<'scores'> is a I. 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. =begin programlisting @@ -139,8 +145,8 @@ X X X -The right-hand side calls a I--a group of behaviors--named C on -the file handle stored in C<$file>. This method reads and returns one line +The right-hand side calls a I--a named group of behavior--named C +on the file handle stored in C<$file>. This method reads and returns one line from the file, removing the line ending. C is also a method, called on the string returned from C. C's single argument is a string containing a space character. C decomposes its I--the string @@ -165,8 +171,8 @@ value that corresponds to a certain C<$key> with C<%hash{$key}>.N 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. Cs and Ces are central enough to each have their own sigil in Perl. The C<$> sigil, however, confers no restriction on the type of the value @@ -312,9 +318,9 @@ X 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). 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.) +value starting at zero. (This is I). 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.) =begin programlisting @@ -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 @@ -337,10 +343,10 @@ X X 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 prints C<2>. +small exception that the return value of the expression is C<$thing> I +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 prints C<2>. =begin programlisting @@ -352,45 +358,44 @@ X X X -This might look a bit scary at first, but it consists of three individually -simple steps. An array's C 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 of the players, not their names. The C method can take an -argument, a I 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 C<$_>. +This line consists of three individually simple steps. An array's C +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 of the players, not their +names. The C method's argument is a I 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 C<$_>. X You have seen blocks before: both the C loop C<< -> $line { ... } >> and the C 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 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 X When two array items have the same value, C leaves them in the same order -as it found them. Computer scientists call this a I. The program +as it found them. Computer scientists call this a I. The program takes advantage of this property of Perl 6's C 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. 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 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 @@ -415,10 +420,10 @@ newline at the end.) X When you run the program, you'll see that C 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. 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. Perl 6 quotes can interpolate variables with the +dollar sigil as well as blocks of code in curly braces. X X @@ -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>.