You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/101-basics.pod6
+21-21Lines changed: 21 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
=beginpod :tag<tutorial> :page-order<A00>
2
2
3
-
=TITLEPerl6 Example P6-101
3
+
=TITLEPerl6 Example P6-101
4
4
5
-
=SUBTITLEA basic introductory example of a Perl6 program
5
+
=SUBTITLEA basic introductory example of a Perl6 program
6
6
7
7
Perl originated as a programming language intended to gather and summarize
8
-
information from text files. It's still strong in text processing, but Perl5
9
-
is also a powerful general-purpose programming language. Perl6 is even better.
8
+
information from text files. It's still strong in text processing, but Perl5
9
+
is also a powerful general-purpose programming language. Perl6 is even better.
10
10
11
11
Suppose that you host a table tennis tournament. The referees tell you the
12
12
results of each game in the format C<Player1 Player2 | 3:2>, which means
@@ -27,7 +27,7 @@ The input data (stored in a file called "scores.txt") looks like this:
27
27
The first line is the list of players. Every subsequent line records a result
28
28
of a match.
29
29
30
-
Here's one way to solve that problem in Perl6:
30
+
Here's one way to solve that problem in Perl6:
31
31
32
32
=begincode
33
33
@@ -75,13 +75,13 @@ This produces the output:
75
75
76
76
=head3X<C<v6>>
77
77
78
-
Every Perl6 program should begin with C<use v6;>. This line tells the compiler
78
+
Every Perl6 program should begin with C<use v6;>. This line tells the compiler
79
79
which version of Perl the program expects. Should you accidentally run the file
80
-
with Perl5, you'll get a helpful error message.
80
+
with Perl5, you'll get a helpful error message.
81
81
82
82
=head3X<C<statement>>
83
83
84
-
A Perl6 program consists of zero or more statements. A I<statement> ends with
84
+
A Perl6 program consists of zero or more statements. A I<statement> ends with
85
85
a semicolon or a curly bracket at the end of a line:
86
86
87
87
=begincode
@@ -142,14 +142,14 @@ the argument provided to C<open>.
142
142
=head3X<C<array>>, X<C<method>> and X<C<invocant>>
143
143
144
144
The right-hand side calls a I<method> --a named group of behavior-- named C<get>
145
-
on the file handle stored in C<$file>. The C<get> method reads and returns
145
+
on the file handle stored in C<$file>. The C<get> method reads and returns
146
146
one line from the file, removing the line ending. If you print the contents of C<$file>
147
-
after calling C<get>, you will see that the first line is no longer in there. C<words> is also a method,
148
-
called on the string returned from C<get>. C<words> decomposes its
147
+
after calling C<get>, you will see that the first line is no longer in there. C<words> is also a method,
148
+
called on the string returned from C<get>. C<words> decomposes its
149
149
I<invocant>--the string on which it operates--into a list of words, which
150
150
here means strings separated by whitespace.
151
-
It turns the single string C<'Beth Ana Charlie Dave'> into the list of
152
-
strings C<'Beth', 'Ana', 'Charlie', 'Dave'>.
151
+
It turns the single string C<'Beth Ana Charlie Dave'> into the list of
152
+
strings C<'Beth', 'Ana', 'Charlie', 'Dave'>.
153
153
154
154
Finally, this list gets
155
155
stored in the array C<@names>. The C<@> sigil marks the declared variable as
@@ -168,8 +168,8 @@ These two lines of code declare two hashes. The C<%> sigil marks each
168
168
variable as a C<Hash>. A C<Hash> is an unordered collection of key-value
169
169
pairs. Other programming languages call that a I<hash table>,
170
170
I<dictionary>, or I<map>. You can query a hash table for the value that
171
-
corresponds to a certain C<$key> with C<%hash{$key}>N<Unlike Perl5, in
172
-
Perl6 the sigil does not change when accessing an array or hash with
171
+
corresponds to a certain C<$key> with C<%hash{$key}>N<Unlike Perl5, in
172
+
Perl6 the sigil does not change when accessing an array or hash with
173
173
C<[ ]> or C<{ }>. This is called X<I<sigil invariance>>.>.
174
174
175
175
In the score counting program, C<%matches> stores the number of matches each
@@ -319,7 +319,7 @@ are passed in through the I<topic variable> C<$_>.
319
319
320
320
You have seen blocks before: both the C<for> loop C<< -> $line { ... } >> and
321
321
the C<if> statement worked on blocks. A block is a self-contained piece of
322
-
Perl6 code with an optional signature (the C<< -> $line >> part). See
322
+
Perl6 code with an optional signature (the C<< -> $line >> part). See
323
323
A<sec:signatures> for more information.
324
324
325
325
The simplest way to sort the players by score would be C<@names.sort({
@@ -332,7 +332,7 @@ tournament.
332
332
333
333
When two array items have the same value, C<sort> leaves them in the same order
334
334
as it found them. Computer scientists call this a I<stable sort>. The program
335
-
takes advantage of this property of Perl6's C<sort> to achieve the goal by
335
+
takes advantage of this property of Perl6's C<sort> to achieve the goal by
336
336
sorting twice: first by the number of sets won (the secondary criterion), then
337
337
by the number of matches won.
338
338
@@ -382,7 +382,7 @@ not interpolate:
382
382
383
383
=endcode
384
384
385
-
Double quoted strings in Perl6 can interpolate variables with the C<$>
385
+
Double quoted strings in Perl6 can interpolate variables with the C<$>
386
386
sigil as well as blocks of code in curly braces. Since any arbitrary
387
387
Perl code can appear within curly braces, C<Array>s and C<Hash>es may be
388
388
interpolated by placing them within curly braces.
@@ -394,12 +394,12 @@ item. Hashes within curly braces are interpolated as a series of
394
394
lines. Each line will contain a key, followed by a tab character, then
395
395
the value associated with that key, and finally a newline.
396
396
397
-
Let's see an example of this now.
397
+
Let's see an example of this now.
398
398
399
399
In this example, you will see some special syntax that makes it easier
400
400
to make a list of strings. This is the C<< <...> >>L<quote-words|https://docs.perl6.org/language/operators#index-entry-qw-quote-words-quote-words> construct.
401
401
When you put words in between the < and > they are all assumed to be strings,
402
-
so you do not need to wrap them each in double quotes C<< "..." >>.
402
+
so you do not need to wrap them each in double quotes C<< "..." >>.
403
403
404
404
=begincode
405
405
@@ -474,7 +474,7 @@ B<2.> Instead of deleting the redundant C<@names> variable, you can also use it
474
474
player appears that wasn't mentioned in the first line, for example due to a
475
475
typo. How would you modify your program to achieve that?
476
476
477
-
Hint: Try using L<membership operators|https://docs.perl6.org/routine/(elem)>.
477
+
Hint: Try using L<membership operators|https://docs.perl6.org/routine/(elem)>.
478
478
479
479
B<Answer:> Change C<@names> to C<@valid-players>. When looping through the lines of
480
480
the file, check to see that C<$p1> and C<$p2> are in C<@valid-players>. Note that
0 commit comments