Hello,
At first, I'm sorry that I'm not good at English.
I'm reading "perlretut" (Perl Regular Expression Tutorial) of version
5.14 now:
http://perldoc.perl.org/perlretut.html
While I was reading "Alternative capture group numbering" section,
I wrote a simple test program to practice it myself.
I'm using Strawberry Perl 5.12.3 on Windows XP.
Here is my code:
-----
#!perl
use strict;
use warnings;
while (1) {
my $input = <STDIN>;
chomp $input;
if ( $input =~ /(?|(a)(b)|(c))(d)/ ) {
print "1[$1] 2[$2] 3[$3]\n";
}
}
-----
Here is the result:
-----
abd
1[a] 2[b] 3[d]
cd
Use of uninitialized value $2 in concatenation (.) or string at d:\Temp
\
test.pl line 13, <STDIN> line 2.
1[c] 2[] 3[d]
----
Okay. This is what I expected and what the document said. 'd' is
assigned to $3 because the maximum number in the alternative numbering
group is 2.
Then I modified the pattern, only changing the order of two group in
the alternative numbering group:
-----
if ( $input =~ /(?|(c)|(a)(b))(d)/ ) {
-----
This is the result:
-----
abd
Use of uninitialized value $3 in concatenation (.) or string at d:\Temp
\
test.pl line 13, <STDIN> line 1.
1[a] 2[d] 3[]
cd
Use of uninitialized value $3 in concatenation (.) or string at d:\Temp
\
test.pl line 13, <STDIN> line 2.
1[c] 2[d] 3[]
----
I have no idea why the result differs from the first one.
Why 'd' is in $2, not $3? Where did 'b' of 'abd' go after matching?
Is this a bug? Or is there something that I misunderstand?
Any help would be appreciated.
Thank you.