I (very hesitantly) say that I think I've found a bug in nested named capture buffers. I've written a minimal sample (see below) that illustrates it. Basically, after the regexp matches, "keys %+" does not return the names of inner-nested capture buffers. However, referencing a specific key, such as $+{inner}, returns the captured value. Any help with either (1) understanding what I'm doing wrong, or (2) how to work around this, would be much appreciated. The code below illustrates the problem. I've listed both the code and its output. I ran this on a MacBook using ActiveState Perl:
mhaines@patru ~/rtx/pshark/cgi-bin/Pshark/AddressParser $ perl -v
This is perl, v5.10.0 built for x86_64-linux-thread-multi-ld
The code:
#!/usr/bin/perl
use strict;

my $inner = qr/(?<inner>inner)/xi;
my $tube = qr/(?<tube>tube)/xi;

my $outer1 = qr/(?<donut>$inner)tube/xi; # $+{inner} set, 'keys %+' doesn't have 'inner'
my $outer2 = qr/(?<donut>$inner $tube)/xi; # same here
my $outer3 = qr/(?<donut>$inner) $tube/xi; # finally %+ has all three keys

&test($outer1, "innertube");
&test($outer2, "innertube");
&test($outer3, "innertube");

sub test {
	my ($regexp, $string) = @_;
	$string =~ $regexp;
	print "\n";
	print "Regexp: $regexp\n";
	print "\tagainst $string\n";
	print "Each:\n";
	while (my ($k, $v) = each %+) {
		print "\t$k = '$v'\n";
	}
	print "Inner: $+{inner}\n";
	print "Tube:  $+{tube}\n";
	print "1: $1\n";
	print "2: $2\n";
	print "3: $3\n";
}
And the output:
Regexp: (?ix-sm:(?<donut>(?ix-sm:(?<inner>inner)))tube)
	against innertube
Each:
	donut = 'inner'
Inner: inner
Tube:  
1: inner
2: inner
3: 

Regexp: (?ix-sm:(?<donut>(?ix-sm:(?<inner>inner)) (?ix-sm:(?<tube>tube))))
	against innertube
Each:
	donut = 'innertube'  <-- Hey!  What about 'inner'???
Inner: inner
Tube:  tube
1: innertube
2: inner
3: tube

Regexp: (?ix-sm:(?<donut>(?ix-sm:(?<inner>inner))) (?ix-sm:(?<tube>tube)))
	against innertube
Each:
	inner = 'inner'
	tube = 'tube'
	donut = 'inner'
Inner: inner
Tube:  tube
1: inner
2: inner
3: tube

In reply to Bug with nested named capture buffers by matthewshark

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.