(?(?{ print "$1 $2 $3\n" })(*FAIL)|(*ACCEPT))
I don't understand the purpose of the (*ACCEPT) false clause in the quoted code fragment. Because the print will always return true (unless there's some terrible I/O failure :), this clause will never be executed. The following versions of the code (with and without (*ACCEPT)) test the same in all 5.10+ Perl versions I have in captivity (see this):
use 5.010; # need (?(?{ code }) pattern)
use Test::More 'no_plan';
use Test::NoWarnings;
note 'perl version ', $];
for my $rw ('.+', '.+ w?') {
for my $rx ('.+', '.+ x?') {
for my $ry ('.+', '(?: . y?)+') {
my $captures = qr{ ($rw) ($rx) ($ry) }xms;
local our @ra;
use re 'eval';
'01234' =~ m{
\A $captures \z
# (?(?{ push @ra, [ $1, $2, $3 ] }) (*F) | (*ACCEPT))
(?(?{ push @ra, [ $1, $2, $3 ] }) (*F))
}xms;
is_deeply \@ra,
[ [ qw(012 3 4) ],
[ qw(01 23 4) ],
[ qw(01 2 34) ],
[ qw(0 123 4) ],
[ qw(0 12 34) ],
[ qw(0 1 234) ],
], $captures;
} # end for $ry
} # end for $rx
} # end for $rw
done_testing;
(Of course, the push statement always returns true.) The true magick seems to reside in the use of the regex conditional expression.
Output:
c:\@Work\Perl\monks\kroki>perl permute_via_regex_1.pl
# perl version 5.014004
ok 1 - (?^msx: (.+) (.+) (.+) )
ok 2 - (?^msx: (.+) (.+) ((?: . y?)+) )
ok 3 - (?^msx: (.+) (.+ x?) (.+) )
ok 4 - (?^msx: (.+) (.+ x?) ((?: . y?)+) )
ok 5 - (?^msx: (.+ w?) (.+) (.+) )
ok 6 - (?^msx: (.+ w?) (.+) ((?: . y?)+) )
ok 7 - (?^msx: (.+ w?) (.+ x?) (.+) )
ok 8 - (?^msx: (.+ w?) (.+ x?) ((?: . y?)+) )
1..8
ok 9 - no warnings
1..9
Of course, your final thought still holds true: none of this is guaranteed against future regex engine optimizations and other "improvements"!
Give a man a fish: <%-{-{-{-<
|