Skip to content

Commit d110c0e

Browse files
authored
Merge pull request #1176 from perl6/language-section-examples
Language section examples reworking (part two)
2 parents af89aa5 + 32b2eb8 commit d110c0e

28 files changed

+475
-195
lines changed

doc/Language/about.pod6

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ example, often contain the methods the class implements.
9393
Definitions must be in one of the following forms to be recognized as
9494
the start of a documentable named, say, Z. First the code in the document source:
9595
96-
=begin code
96+
=begin code :skip-test
9797
9898
=item X<C<How to use the Z infix> | infix,Z> (This a special case, which
9999
is always considered a definition)
@@ -131,7 +131,7 @@ You can add emphasis with bold (B<V< B<> >>) or italicized (B<V< I<> >>),
131131
with or without code formatting (B<V< C<> >>). Due to current parser limitations,
132132
special steps have to be taken to use B<V< X<> >> with other formatting codes; for example:
133133
134-
=begin code
134+
=begin code :skip-test
135135
=item X<B<foo>|foo> a fancy subroutine
136136
=end code
137137

doc/Language/classtut.pod6

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ key principles of object oriented design.
145145
The first declaration specifies instance storage for a callback – a bit of
146146
code to invoke in order to perform the task that an object represents:
147147
148-
has &!callback;
148+
=for code :skip-test
149+
has &!callback;
149150
150151
X<|sigils,&>
151152
X<|twigils>
@@ -158,7 +159,8 @@ this attribute is private to the class.
158159
159160
The second declaration also uses the private twigil:
160161
161-
has Task @!dependencies;
162+
=for code :skip-test
163+
has Task @!dependencies;
162164
163165
However, this attribute represents an array of items, so it requires the
164166
C<@> sigil. These items each specify a task that must be completed before
@@ -168,7 +170,8 @@ class (or some subclass of it).
168170
169171
The third attribute represents the state of completion of a task:
170172
171-
has Bool $.done;
173+
=for code :skip-test
174+
has Bool $.done;
172175
173176
X<|twigils,.>
174177
X<|twigils,accessors>
@@ -210,26 +213,27 @@ those with and without accessors):
210213
The assignment is carried out at object build time. The right-hand side is
211214
evaluated at that time, and can even reference earlier attributes:
212215
213-
has Task @!dependencies;
214-
has $.ready = not @!dependencies;
216+
=for code :skip-test
217+
has Task @!dependencies;
218+
has $.ready = not @!dependencies;
215219
216220
=head1 Static fields?
217221
218222
Perl 6 has no B<static> keyword. Nevertheless, any class may declare anything
219223
that a module can, so making a scoped variable sounds like good idea.
220224
221-
=begin code
225+
=begin code
222226
223-
class Singleton {
224-
my Singleton $instance;
225-
method new {!!!}
226-
submethod instance {
227-
$instance = Singleton.bless unless $instance;
228-
$instance;
229-
}
230-
}
227+
class Singleton {
228+
my Singleton $instance;
229+
method new {!!!}
230+
submethod instance {
231+
$instance = Singleton.bless unless $instance;
232+
$instance;
233+
}
234+
}
231235
232-
=end code
236+
=end code
233237
234238
Class attributes defined by L<my|/syntax/my> or L<our|/syntax/our> may also be initialized when
235239
being declared, however we are implementing the Singleton pattern here and
@@ -238,9 +242,11 @@ predict the moment when attribute initialization will be executed, because
238242
it can take place during compilation, runtime or both, especially when
239243
importing the class using the L<use|/syntax/use> keyword.
240244
245+
=begin code :skip-test
241246
class HaveStaticAttr {
242247
my Foo $.foo = some_complicated_subroutine;
243248
}
249+
=end code
244250
245251
Class attributes may also be declared with a secondary sigil – in a similar
246252
manner to object attributes – that will generate read-only accessors if the
@@ -256,9 +262,11 @@ ignore the C<new> method temporarily; it's a special type of method.
256262
Consider the second method, C<add-dependency>, which adds a new task to a
257263
task's dependency list.
258264
265+
=begin code :skip-test
259266
method add-dependency(Task $dependency) {
260267
push @!dependencies, $dependency;
261268
}
269+
=end code
262270
263271
X<|invocant>
264272
@@ -274,13 +282,15 @@ attribute.
274282
275283
The C<perform> method contains the main logic of the dependency handler:
276284
285+
=begin code :skip-test
277286
method perform() {
278287
unless $!done {
279288
.perform() for @!dependencies;
280289
&!callback();
281290
$!done = True;
282291
}
283292
}
293+
=end code
284294
285295
It takes no parameters, working instead with the object's attributes. First,
286296
it checks if the task has already completed by checking the C<$!done>
@@ -377,7 +387,8 @@ allowed to bind things to C<&!callback> and C<@!dependencies> directly. To
377387
do this, we override the C<BUILD> submethod, which is called on the brand
378388
new object by C<bless>:
379389
380-
submethod BUILD(:&!callback, :@!dependencies) { }
390+
=for code :skip-test
391+
submethod BUILD(:&!callback, :@!dependencies) { }
381392
382393
Since C<BUILD> runs in the context of the newly created C<Task> object, it
383394
is allowed to manipulate those private attributes. The trick here is that
@@ -389,6 +400,7 @@ more information.
389400
The C<BUILD> method is responsible for initializing all attributes and must also
390401
handle default values:
391402
403+
=begin code :skip-test
392404
has &!callback;
393405
has @!dependencies;
394406
has Bool ($.done, $.ready);
@@ -398,6 +410,7 @@ handle default values:
398410
:$!done = False,
399411
:$!ready = not @!dependencies
400412
) { }
413+
=end code
401414
402415
See L<Object Construction|/language/objects#Object_Construction> for more
403416
options to influence object construction and attribute initialization.
@@ -408,7 +421,8 @@ After creating a class, you can create instances of the class. Declaring a
408421
custom constructor provides a simple way of declaring tasks along with their
409422
dependencies. To create a single task with no dependencies, write:
410423
411-
my $eat = Task.new({ say 'eating dinner. NOM!' });
424+
=for code :skip-test
425+
my $eat = Task.new({ say 'eating dinner. NOM!' });
412426
413427
An earlier section explained that declaring the class C<Task> installed a
414428
type object in the namespace. This type object is a kind of "empty
@@ -419,6 +433,7 @@ modifying or accessing an existing object.
419433
420434
Unfortunately, dinner never magically happens. It has dependent tasks:
421435
436+
=begin code :skip-test
422437
my $eat =
423438
Task.new({ say 'eating dinner. NOM!' },
424439
Task.new({ say 'making dinner' },
@@ -429,18 +444,21 @@ Unfortunately, dinner never magically happens. It has dependent tasks:
429444
Task.new({ say 'cleaning kitchen' })
430445
)
431446
);
447+
=end code
432448
433449
Notice how the custom constructor and sensible use of whitespace makes task dependencies clear.
434450
435451
Finally, the C<perform> method call recursively calls the C<perform> method
436452
on the various other dependencies in order, giving the output:
437453
454+
=begin code :skip-test
438455
making some money
439456
going to the store
440457
buying food
441458
cleaning kitchen
442459
making dinner
443460
eating dinner. NOM!
461+
=end code
444462
445463
=head1 Inheritance
446464
@@ -477,7 +495,7 @@ other means, such as attribute accessors.
477495
Now, any object of type Programmer can make use of the methods and accessors
478496
defined in the Employee class as though they were from the Programmer class.
479497
480-
=begin code
498+
=begin code :skip-test
481499
my $programmer = Programmer.new(
482500
salary => 100_000,
483501
known_languages => <Perl5 Perl6 Erlang C++>,
@@ -494,7 +512,7 @@ Of course, classes can override methods and attributes defined by parent
494512
classes by defining their own. The example below demonstrates the C<Baker>
495513
class overriding the C<Cook>'s C<cook> method.
496514
497-
=begin code
515+
=begin code :skip-test
498516
class Cook is Employee {
499517
has @.utensils is rw;
500518
has @.cookbooks is rw;
@@ -545,7 +563,7 @@ algorithm to linearize multiple inheritance hierarchies, which is a
545563
significant improvement over Perl 5's default approach
546564
(depth-first search) to handling multiple inheritance.
547565
548-
=begin code
566+
=begin code :skip-test
549567
class GeekCook is Programmer is Cook {
550568
method new( *%params ) {
551569
push( %params<cookbooks>, "Cooking for Geeks" );
@@ -580,6 +598,7 @@ Classes to be inherited from can be listed in the class declaration body by
580598
prefixing the C<is> trait with C<also>. This also works for the role
581599
composition trait C<does>.
582600
601+
=begin code :skip-test
583602
class GeekCook {
584603
also is Programmer;
585604
also is Cook;
@@ -589,6 +608,7 @@ composition trait C<does>.
589608
role A {};
590609
role B {};
591610
class C { also does A; also does B }
611+
=end code
592612
593613
=head1 Introspection
594614
@@ -599,21 +619,25 @@ a controlling object) for some properties, such as its type.
599619
Given an object C<$o> and the class definitions from the previous sections,
600620
we can ask it a few questions:
601621
622+
=begin code :skip-test
602623
if $o ~~ Employee { say "It's an employee" };
603624
if $o ~~ GeekCook { say "It's a geeky cook" };
604625
say $o.WHAT;
605626
say $o.perl;
606627
say $o.^methods(:local)».name.join(', ');
607628
say $o.^name;
629+
=end code
608630
609631
The output can look like this:
610632
633+
=begin code :skip-test
611634
It's an employee
612635
(Programmer)
613636
Programmer.new(known_languages => ["Perl", "Python", "Pascal"],
614637
favorite_editor => "gvim", salary => "too small")
615638
code_to_solve, known_languages, favorite_editor
616639
Programmer
640+
=end code
617641
618642
The first two tests each smart-match against a class name. If the object is
619643
of that class, or of an inheriting class, it returns true. So the object in
@@ -640,8 +664,9 @@ it is actually a method call on its I<meta class>, which is a class managing
640664
the properties of the C<Programmer> class – or any other class you are
641665
interested in. This meta class enables other ways of introspection too:
642666
643-
say $o.^attributes.join(', ');
644-
say $o.^parents.map({ $_.^name }).join(', ');
667+
=for code :skip-test
668+
say $o.^attributes.join(', ');
669+
say $o.^parents.map({ $_.^name }).join(', ');
645670
646671
Finally C<$o.^name> calls the C<name> method on the meta object, which
647672
unsurprisingly returns the class name.

doc/Language/concurrency.pod6

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,13 @@ to the C<map> is emitted:
374374
If you need to have an action that runs when the supply finishes, you can do so
375375
by setting the C<done> and C<quit> options in the call to C<tap>:
376376
377+
=begin code :skip-test
377378
$supply.tap: { ... },
378379
done => { say 'Job is done.' },
379380
quit => {
380381
when X::MyApp::Error { say "App Error: ", $_.message }
381382
};
383+
=end code
382384
383385
The C<quit> block works very similar to a C<CATCH>. If the exception is marked
384386
as seen by a C<when> or C<default> block, the exception is caught and handled.
@@ -390,13 +392,15 @@ If you are using the C<react> or C<supply> block syntax with C<whenever>, you
390392
can add phasers within your C<whenever> blocks to handle the C<done> and C<quit>
391393
messages from the tapped supply:
392394
395+
=begin code :skip-test
393396
react {
394397
whenever $supply {
395398
...; # your usual supply tap code here
396399
LAST { say 'Job is done.' }
397400
QUIT { when X::MyApp::Error { say "App Error: ", $_.message } }
398401
}
399402
}
403+
=end code
400404
401405
The behavior here is the same as setting C<done> and C<quit> on C<tap>.
402406
@@ -492,6 +496,7 @@ The C<.poll> method can be used in combination with C<.receive> method, as a
492496
caching mechanism where lack of value returned by `.poll` is a signal that
493497
more values need to be fetched and loaded into the channel:
494498
499+
=begin code :skip-test
495500
sub get-value {
496501
return $c.poll // do { start replenish-cache; $c.receive };
497502
}
@@ -501,10 +506,12 @@ more values need to be fetched and loaded into the channel:
501506
$c.send: $_ for slowly-fetch-a-thing();
502507
}
503508
}
509+
=end code
504510
505511
Channels can be used in place of the L<Supply> in the C<whenever> of a
506512
C<react> block described earlier:
507513
514+
=begin code :skip-test
508515
my $channel = Channel.new;
509516
my $p = start {
510517
react {
@@ -523,6 +530,7 @@ C<react> block described earlier:
523530
524531
$channel.close;
525532
await $p;
533+
=end code
526534
527535
It is also possible to obtain a L<Channel> from a L<Supply> using the
528536
L<Channel method|/type/Supply#method_Channel> which returns a L<Channel>
@@ -642,7 +650,8 @@ In both cases the completion of the code encapsulated by the L<Thread>
642650
object can be waited on with the C<finish> method which will block until
643651
the thread completes:
644652
645-
$thread.finish;
653+
=for code :skip-test
654+
$thread.finish;
646655
647656
Beyond that there are no further facilities for synchronization or resource
648657
sharing which is largely why it should be emphasized that threads are unlikely

doc/Language/exceptions.pod6

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ In other words, even when the exception is handled successfully, the I<rest of t
8888
8989
Output:
9090
91-
something went wrong ...
91+
=for code :skip-test
92+
something went wrong ...
9293
9394
Compare with this:
9495
@@ -102,11 +103,7 @@ Compare with this:
102103
103104
}
104105
105-
say "Hi! I am at the outer block!";
106-
107-
Output:
108-
109-
Hi! I am at the outer block!
106+
say "Hi! I am at the outer block!"; # OUTPUT: «Hi! I am at the outer block!␤»
110107
111108
See "Resuming of Exceptions", for how to return control back to where the exception originated.
112109
@@ -143,19 +140,23 @@ rethrown.
143140
144141
Output:
145142
143+
=begin code :skip-test
146144
I'm alive!
147145
No, I expect you to DIE Mr. Bond!
148146
I'm immortal.
149147
Just stop already!
150148
in block <unit> at exception.p6 line 21
149+
=end code
151150
152151
A C<try>-block is a normal block and as such treats it's last statement as the
153152
return value of itself. We can therefore use it as a RHS.
154153
155-
say try { +"99999" } // "oh no"
156-
say try { +"hello" } // "oh no"
154+
=begin code
155+
say try { +"99999" } // "oh no";
156+
say try { +"hello" } // "oh no";
157157
158158
# OUTPUT«99999␤oh no␤»
159+
=end code
159160
160161
=head1 Throwing exceptions
161162

0 commit comments

Comments
 (0)