Skip to content

Commit 08b6365

Browse files
committed
Add simple trap example for Captures
1 parent 7f6e215 commit 08b6365

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/Language/traps.pod

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ inside C<BUIDLALL>, but not inside C<BUILD>).
109109
110110
=head2 Whitespace in Regexes does not match literally
111111
112-
$ perl6-e "say 'a b' ~~ /a b/"
112+
$ perl6 -e "say 'a b' ~~ /a b/"
113113
False
114114
115115
Whitespace in regexes is, by default, considered an optional filler without
@@ -123,5 +123,24 @@ Ways to match whitespace:
123123
=item C<\h>, C<\v> for horizontal, vertical whitespace
124124
=item with C<m:s/a b/> or C<m:sigspace/a b/>, the blank in the regexes matches arbitrary whitespace
125125
126+
=head1 Captures
126127
128+
=head2 Variables versus values/expressions in a Capture
129+
130+
Beginners might expect a variable in a C<Capture> to supply its current
131+
value when that C<Capture> is later used. For example:
132+
133+
$perl6 -e 'my $a = 2; say($a, ++$a)'
134+
33
135+
136+
Here the C<Capture> contained the B<variable> C<$a> and the B<value> of the result
137+
of the expression C<++$a>. Since the C<Capture> must be reified before C<&say> can
138+
use it, the C<++$a> may happen before C<&say> gets C<$a>, and so it may already
139+
be incremented.
140+
141+
Instead, use an expression that produces a value when you want a value.
142+
143+
$perl6 -e 'my $a = 2; say(+$a, ++$a)'
144+
23
145+
127146
=end pod

0 commit comments

Comments
 (0)