Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Strange behavior of @- and @+ in perl5.10 regexps

by JavaFan (Canon)
on Sep 11, 2009 at 10:54 UTC ( #794744=note: print w/ replies, xml ) Need Help??


in reply to Strange behavior of @- and @+ in perl5.10 regexps

I don't think @- and @+ are guaranteed to contain anything meaningful during a match, so it would be hard to say there's a bug.


Comment on Re: Strange behavior of @- and @+ in perl5.10 regexps
Re^2: Strange behavior of @- and @+ in perl5.10 regexps
by casiano (Pilgrim) on Sep 11, 2009 at 11:16 UTC
    Thanks JavaFan,

    ... I don't think @- and @+ are guaranteed to contain anything meaningful during a match
    But it would be useful if they do so. They will give you the opportunity to access the attributes of previous sections inside embedded code (see ikegami answer in node Backreference variables in code embedded inside Perl 5.10 regexps) and mimic Parse::Recdescent programming style.
      But it would be useful if they do so.
      Yes, but that doesn't mean the current (non)behaviour is a bug.

      Wishes are nice, but at the moment there's only just one guy doing any serious work on the regexp engine. And he's swamped already.

      Patches will always be welcome.

        javafan,

        Thanks for your comments

        ... I don't think @- and @+ are guaranteed to contain anything meaningful during a match, so it would be hard to say there's a bug.
        Observe that the main anomaly, i.e., the fact that @- and @+ have different lengths occurs at the end of the regexp parsing, not during the intermediate process. See the following variant of the former example:
        pl@nereida:~/Lperltesting$ cat -n ./offsetsin5_10_end.pl 1 #!/usr/local/lib/perl/5.10.1/bin//perl5.10.1 2 use v5.10; 3 4 my $input; 5 6 local $" = ", "; 7 8 my $parser = qr{ 9 ^ 10 ((?&expr)) 11 ((?&expr)) 12 \z 13 (?{ 14 say "main:\n\@- = (@-)\t\t ".scalar(@-)." items\n +\@+ = (@+)\t ".scalar(@+)." items\n"; 15 }) 16 17 (?(DEFINE) 18 (?<expr> 19 (.) (.) 20 (?{ 21 say "expr:\n\@- = (@-)\t ".scalar(@-)." items +\n\@+ = (@+)\t ".scalar(@+)." items\n"; 22 }) 23 ) 24 ) 25 }x; 26 27 $input = <>; 28 chomp($input); 29 if ($input =~ $parser) { 30 say "matches: ($&)"; 31 say "At the very end:\n\@- = (@-)\t ".scalar(@-)." items\n\@ ++ = (@+)\t ".scalar(@+)." items\n"; 32 }
        The only new line is line 31. When executed says:
        pl@nereida:~/Lperltesting$ ./offsetsin5_10_end.pl abab expr: @- = (0, , , , 0, 1) 6 items @+ = (2, , , , 1, 2) 6 items expr: @- = (0, 0, , , 2, 3) 6 items @+ = (4, 2, , , 3, 4) 6 items main: @- = (0, 0, 2) 3 items @+ = (4, 2, 4, , , ) 6 items matches: (abab) At the very end: @- = (0, 0, 2) 3 items @+ = (4, 2, 4, , , ) 6 items
        Apologies for not being able to help sending a patch. I would like to have the required knowledge to do it.

Re^2: Strange behavior of @- and @+ in perl5.10 regexps
by demerphq (Chancellor) on Sep 13, 2009 at 13:08 UTC

    Hmm, probably not documented directly and might not be tested explicitly. But certainly indirectly. We have lots of tests that $1 and friends behave "as expected" inside of (?{ ... }) and (??{ ... }) blocks. So effectively that means that @- and @+ have to as well, as they are all just ties into the same C level data structures.

    Now, at a certain level these constructs are still documented as experimental or subject to change so technically you have a point, and I appreciate that you pointed this out.

    But I personally would/do see problems with the magic variables inside of these constructs as a bugs, the experimental status just says I get to change my mind if I want. :-) However in this case things are working pretty much exactly as planned, with the possible nit as to whether (?<expr> ... ) should have a slot allocated to it that never gets used. Which is mostly irritating as it is wasteful, and a little counter-intuitive, but actually expected behaviour.

    ---
    $world=~s/war/peace/g

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://794744]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-09-16 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (16 votes), past polls