Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Perl6ish rules in Perl5's regex engine

by ysth (Canon)
on Sep 10, 2006 at 16:07 UTC ( [id://572229]=note: print w/replies, xml ) Need Help??


in reply to Perl6ish rules in Perl5's regex engine

and requires only one compiled regexp per pattern, instead of many as the current scheme dictates (embedding a qr// in a larger pattern results in a complete recompile).
It was my understanding that this wasn't true - that no recompile took place when nesting with (??{}), and testing seems to bear this out:
$ perl -we'use re "debug"; $x=qr/(.)\1|(.)(??{$x})\2/; "abcddcba" =~ / +^$x\z/' Compiling REx `(.)\1|(.)(??{$x})\2' size 20 Got 164 bytes for offset annotations. 1: BRANCH(9) 2: OPEN1(4) 4: REG_ANY(5) 5: CLOSE1(7) 7: REF1(20) 9: BRANCH(20) 10: OPEN2(12) 12: REG_ANY(13) 13: CLOSE2(15) 15: LOGICAL[2](16) 16: EVAL(18) 18: REF2(20) 20: END(0) minlen 1 with eval Offsets: [20] 0[0] 1[1] 0[0] 2[1] 3[1] 0[0] 4[2] 0[0] 6[1] 7[1] 0[0] 8[1] 9[1] 0 +[0] 17[0] 17[0] 0[0] 18[2] 0[0] 20[0] Compiling REx `^(?-xism:(.)\1|(.)(??{$x})\2)\z' size 23 Got 188 bytes for offset annotations. first at 2 1: BOL(2) 2: BRANCH(10) 3: OPEN1(5) 5: REG_ANY(6) 6: CLOSE1(8) 8: REF1(22) 10: BRANCH(21) 11: OPEN2(13) 13: REG_ANY(14) 14: CLOSE2(16) 16: LOGICAL[2](17) 17: EVAL(19) 19: REF2(22) 21: TAIL(22) 22: EOS(23) 23: END(0) floating `'$ at 1..2147483647 (checking floating) anchored(BOL) minlen + 1 with eval Offsets: [23] 1[1] 9[1] 10[1] 0[0] 11[1] 12[1] 0[0] 13[2] 0[0] 15[1] 16[1] 0[0] +17[1] 18[1] 0[0] 26[0] 26[0] 0[0] 27[2] 0[0] 28[0] 30[2] 32[0] Guessing start of match, REx `^(?-xism:(.)\1|(.)(??{$x})\2)\z' against + `abcddcba'... Found floating substr `'$ at offset 8... Guessed: match at offset 0 Matching REx `^(?-xism:(.)\1|(.)(??{$x})\2)\z' against `abcddcba' Setting an EVAL scope, savestack=15 0 <> <abcddcba> | 1: BOL 0 <> <abcddcba> | 2: BRANCH Setting an EVAL scope, savestack=21 0 <> <abcddcba> | 3: OPEN1 0 <> <abcddcba> | 5: REG_ANY 1 <a> <bcddcba> | 6: CLOSE1 1 <a> <bcddcba> | 8: REF1 failed... 0 <> <abcddcba> | 11: OPEN2 0 <> <abcddcba> | 13: REG_ANY 1 <a> <bcddcba> | 14: CLOSE2 1 <a> <bcddcba> | 16: LOGICAL[2] 1 <a> <bcddcba> | 17: EVAL re_eval 0x1003bb30 Entering embedded `(.)\1|(.)(??{$x})\2' Setting an EVAL scope, savestack=35 1 <a> <bcddcba> | 1: BRANCH Setting an EVAL scope, savestack=41 1 <a> <bcddcba> | 2: OPEN1 1 <a> <bcddcba> | 4: REG_ANY 2 <ab> <cddcba> | 5: CLOSE1 2 <ab> <cddcba> | 7: REF1 failed... 1 <a> <bcddcba> | 10: OPEN2 1 <a> <bcddcba> | 12: REG_ANY 2 <ab> <cddcba> | 13: CLOSE2 2 <ab> <cddcba> | 15: LOGICAL[2] 2 <ab> <cddcba> | 16: EVAL re_eval 0x10019550 Entering embedded `(.)\1|(.)(??{$x})\2' Setting an EVAL scope, savestack=55 2 <ab> <cddcba> | 1: BRANCH Setting an EVAL scope, savestack=61 2 <ab> <cddcba> | 2: OPEN1 2 <ab> <cddcba> | 4: REG_ANY 3 <abc> <ddcba> | 5: CLOSE1 3 <abc> <ddcba> | 7: REF1 failed... 2 <ab> <cddcba> | 10: OPEN2 2 <ab> <cddcba> | 12: REG_ANY 3 <abc> <ddcba> | 13: CLOSE2 3 <abc> <ddcba> | 15: LOGICAL[2] 3 <abc> <ddcba> | 16: EVAL re_eval 0x10019550 Entering embedded `(.)\1|(.)(??{$x})\2' Setting an EVAL scope, savestack=75 3 <abc> <ddcba> | 1: BRANCH Setting an EVAL scope, savestack=81 3 <abc> <ddcba> | 2: OPEN1 3 <abc> <ddcba> | 4: REG_ANY 4 <abcd> <dcba> | 5: CLOSE1 4 <abcd> <dcba> | 7: REF1 5 <abcdd> <cba> | 20: END Setting an EVAL scope, savestack=95 restoring \1 to 2(2)..-1 restoring \2 to 2(2)..3 5 <abcdd> <cba> | 18: REF2 6 <abcddc> <ba> | 20: END Setting an EVAL scope, savestack=109 restoring \1 to 1(1)..-1 restoring \2 to 1(1)..2 6 <abcddc> <ba> | 18: REF2 7 <abcddcb> <a> | 20: END Setting an EVAL scope, savestack=123 restoring \1 to 0(0)..-1 restoring \2 to 0(0)..1 7 <abcddcb> <a> | 19: REF2 8 <abcddcba> <> | 22: EOS 8 <abcddcba> <> | 23: END Match successful! Freeing REx: `"^(?-xism:(.)\\1|(.)(??{$x})\\2)\\z"' Freeing REx: `"(.)\\1|(.)(??{$x})\\2"'

Replies are listed 'Best First'.
Re^2: Perl6ish rules in Perl5's regex engine
by demerphq (Chancellor) on Sep 10, 2006 at 16:13 UTC

    In this case there are two patterns (as your output shows). The first is the precompiled $qr and the second is that formed by concatenating the $qr into a larger pattern. You are correct that when processing the (??{$qr}) there is no recompile tho. When I said "many" I was thinking of scenarios where you would have multiple possible sub patterns (or rules), or where the (??{...}) returned a string instead of a pattern.

    BTW, note it does eval the code everytime.... Even if it doesnt recompile the pattern.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found