#!/usr/bin/perl
use re "eval";
# (?{ CODE }) is a classical zero-width assertion
# that allways succeeds and it is used only for its
# side-effects.
my $non_zero_width = 'a(?{ print 1 })';
my $zero_width = '(?{ print 2 })';
# These two should be equivalent according to perlre.
my $re1 = qq/ (?: $non_zero_width | $zero_width )* /;
my $re2 = qq/ (?: $non_zero_width )* | (?: $zero_width )? /;
# But are they really?
$_ = 'aaabbb';
print "\n-----------------\n";
/$re1/x;
print "\n-----------------\n";
/$re2/x;
print "\n-----------------\n";
The output is:
-----------------
1112
-----------------
111
-----------------
which proves my point.
|