Skip to content

Commit 64b148b

Browse files
committed
Document the flipflop operators
I wasn't sure as to the level of detail desired on the operator page, so hopefully it's not too much or too little for these eight operators. These operators feel like they could benefit from an(other,) independent page explaining things in a bit more detail anyway.
1 parent ff84848 commit 64b148b

File tree

1 file changed

+157
-1
lines changed

1 file changed

+157
-1
lines changed

lib/Language/operators.pod

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,163 @@ C<$condition ?? $true !! $false> evaluates and returns the expression from the
11221122
C<$true> branch if C<$condition> is a true value. Otherwise it evaluates and
11231123
returns the C<$false> branch.
11241124
1125-
# TODO: ff, ^ff, ff^, ^ff^, fff, ^fff, fff^, ^fff^
1125+
=head2 infix C«ff»
1126+
1127+
sub infix:<ff>(Mu $a, Mu $b)
1128+
1129+
Flipflop operator.
1130+
1131+
Compares both arguments to C<$_> (that is, C<$_ ~~ $a> and C<$_ ~~
1132+
$b>). Evaluates to C<False> until the left-hand smartmatch is C<True>, at which
1133+
point it evaluates to C<True> until the right-hand smartmatch is C<True>.
1134+
1135+
In effect, the left-hand argument is the "start" condition, and the right-hand
1136+
is the "stop" condition. This construct is typically used to pick up only a
1137+
certain section of lines. For example:
1138+
1139+
=begin code :allow<B V>
1140+
my $excerpt = q:to/END/;
1141+
Here's some unimportant text.
1142+
V<=>begin code
1143+
This code block is what we're after.
1144+
We'll use 'ff' to get it.
1145+
V<=>end code
1146+
More unimportant text.
1147+
END
1148+
1149+
my @codelines = gather for $excerpt.lines {
1150+
take $_ if B<"=begin code" ff "=end code">
1151+
}
1152+
1153+
# this will print four lines, starting with "=begin code" and ending with
1154+
# "=end code"
1155+
say @codelines.join("\n");
1156+
=end code
1157+
1158+
After matching the start condition, the operator will then match the same C<$_>
1159+
to the stop condition, and act accordingly if successful. In this example, only
1160+
the first element is printed:
1161+
1162+
for <AB C D B E F> {
1163+
say $_ if /A/ ff /B/; # prints only "AB"
1164+
}
1165+
1166+
For the sed-like version, which does I<not> try C<$_> on the stop condition
1167+
after succeeding on the start condition, see L<C<fff>>.
1168+
1169+
This operator cannot be overloaded, as it is handled specially by the compiler.
1170+
1171+
=head2 infix C«^ff»
1172+
1173+
sub infix:<^ff>(Mu $a, Mu $b)
1174+
1175+
Works like L<C<ff>>, except it does not return C<True> for items matching the
1176+
start condition (including items also matching the stop condition).
1177+
1178+
A comparison:
1179+
1180+
my @list = <A B C>;
1181+
say $_ if /A/ ff /C/ for @list; # prints A, B, and C
1182+
say $_ if /A/ ^ff /C/ for @list; # prints B and C
1183+
1184+
The sed-like version can be found in L<C<^fff>>.
1185+
1186+
This operator cannot be overloaded, as it is handled specially by the compiler.
1187+
1188+
=head2 infix C«ff^»
1189+
1190+
sub infix:<ff^>(Mu $a, Mu $b)
1191+
1192+
Works like L<C<ff>>, except it does not return C<True> for items matching the
1193+
stop condition (including items that first matched the start condition).
1194+
1195+
my @list = <A B C>;
1196+
say $_ if /A/ ff /C/ for @list; # prints A, B, and C
1197+
say $_ if /A/ ff^ /C/ for @list; # prints A and B
1198+
1199+
The sed-like version can be found in L<C<fff^>>.
1200+
1201+
This operator cannot be overloaded, as it is handled specially by the compiler.
1202+
1203+
=head2 infix C«^ff^»
1204+
1205+
sub infix:<^ff^>(Mu $a, Mu $b)
1206+
1207+
Works like L<C<ff>>, except it does not return C<True> for items matching either
1208+
the stop or start condition (or both).
1209+
1210+
my @list = <A B C>;
1211+
say $_ if /A/ ff /C/ for @list; # prints A, B, and C
1212+
say $_ if /A/ ^ff^ /C/ for @list; # prints B
1213+
1214+
The sed-like version can be found in L<C<^fff^>>.
1215+
1216+
This operator cannot be overloaded, as it is handled specially by the compiler.
1217+
1218+
=head2 infix C«fff»
1219+
1220+
sub infix:<fff>(Mu $a, Mu $b)
1221+
1222+
Performs a sed-like flipflop operation, wherein it returns C<False> until the
1223+
left argument smartmatches against C<$_>, and after that returns C<True> until
1224+
the right argument smartmatches against C<$_>.
1225+
1226+
Works similarly to L<C<ff>>, except that it only tries one argument per
1227+
invocation. That is, if C<$_> smartmatches the left argument, C<fff> will B<not>
1228+
then try to match that same C<$_> against the right argument.
1229+
1230+
for <AB C D B E F> {
1231+
say $_ if /A/ fff /B/; # Prints "AB", "C", "D", and "B"
1232+
}
1233+
1234+
The non-sed-like flipflop (which after succesfully matching the left argument
1235+
against C<$_> will try that same C<$_> against the right argument and act
1236+
accordingly), see L<C<ff>>.
1237+
1238+
This operator cannot be overloaded, as it is handled specially by the compiler.
1239+
1240+
=head2 infix C«^fff»
1241+
1242+
sub infix:<^fff>(Mu $a, Mu $b)
1243+
1244+
Like L<C<fff>>, except it does not return true for matches to the left argument.
1245+
1246+
my @list = <A B C>;
1247+
say $_ if /A/ fff /C/ for @list; # prints A, B, and C
1248+
say $_ if /A/ ^fff /C/ for @list; # prints B and C
1249+
1250+
For the non-sed version, see L<C<^ff>>.
1251+
1252+
This operator cannot be overloaded, as it is handled specially by the compiler.
1253+
1254+
=head2 infix C«fff^»
1255+
1256+
sub infix:<fff^>(Mu $a, Mu $b)
1257+
1258+
Like L<C<fff>>, except it does not return true for matches to the right argument.
1259+
1260+
my @list = <A B C>;
1261+
say $_ if /A/ fff /C/ for @list; # prints A, B, and C
1262+
say $_ if /A/ fff^ /C/ for @list; # prints A and B
1263+
1264+
For the non-sed version, see L<C<ff^>>.
1265+
1266+
This operator cannot be overloaded, as it is handled specially by the compiler.
1267+
1268+
=head2 infix C«^fff^»
1269+
1270+
sub infix:<^fff^>(Mu $a, Mu $b)
1271+
1272+
Like L<C<fff>>, except it does not return true for matches to either the left or
1273+
right argument.
1274+
1275+
my @list = <A B C>;
1276+
say $_ if /A/ fff /C/ for @list; # prints A, B, and C
1277+
say $_ if /A/ ^fff^ /C/ for @list; # prints B
1278+
1279+
For the non-sed version, see L<C<^ff^>>.
1280+
1281+
This operator cannot be overloaded, as it is handled specially by the compiler.
11261282
11271283
=head1 Item Assignment Precedence
11281284

0 commit comments

Comments
 (0)