Perhaps you should put in your docs that use re 'eval'; is required to get this to work - it complained when I tried without that. ;-)

Also, I tried the following:

use Regexp::NamedCaptures; use re 'eval'; my $s = 'blah bbbbbbbb baz'; my $foo; my $q = Regexp::NamedCaptures::convert('blah (?<$foo>\w+) (\w+)(?:\s+( +\w+))'); print $q,"\n"; my @x = $s =~ qr/$q/; print "foo = $foo\n\@x = ", join(', ',map { "[$_]" } @x), "\n";
just to try it out. I expected @x to contain 'baz'. Or to contain 'bbbbbbbb', 'baz'. I wasn't sure which. I got just 'bbbbbbbb'. Looking at the first line, $q, I see that convert managed to stop after the (?<$foo>\w+) portion - everything after that close-parenthesis was dropped.

Thought you might like to know ;-)

Using perl 5.8.6 on linux.

Update: Two fixes. First, my code. That final (?:\s+(\w+)) needs a ? afterwards (an optional capture). Second, the original module: we need to insert else { $out .= $expression } at line 105 - after the if ( '(?<' ... block. Note that without the first fix, the regular expression doesn't match, but $foo gets set anyway. We probably need a way to back out the changes if the expression fails.

I'll leave you with this harder bug. ;-) However, that does mean that this does not quite behave as advertised - $foo is getting set even if the match fails, thus the "if (/.../) { $foo = $1 }" example in your perldoc is not quite describing how it behaves.


In reply to Re: Regexp::NamedCaptures, at last by Tanktalus
in thread Regexp::NamedCaptures, at last by diotalevi

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.