Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

SBECK:

In that case, go ahead and recompile the regex in the debugger, like this:

do { use re qw(Debug More); $re1=qr/g/; }

We simply create a block and inside it, we turn on regex debug mode, and compile the regex.

Here's the test run I did:

$ cat t.pl use strict; use warnings; my $re1 = qr/p/; my $re2 = qr/t/; use re qw(Debug More); my @list = ('alpha', 'beta', 'gamma', 'delta'); for (@list) { print $_, "\n"; if (/$re1/) { print "has P\n" } if (/$re2/) { print "has T\n" } } $ perl -d t.pl <<< snip >>> main::(t.pl:4): my $re1 = qr/p/; DB<1> s main::(t.pl:5): my $re2 = qr/t/; DB<1> main::(t.pl:9): my @list = ('alpha', 'beta', 'gamma', 'delta'); DB<1> main::(t.pl:11): for (@list) { DB<1> main::(t.pl:12): print $_, "\n"; DB<1> alpha main::(t.pl:13): if (/$re1/) { print "has P\n" } DB<1> main::(t.pl:13): if (/$re1/) { print "has P\n" } DB<1> has P main::(t.pl:14): if (/$re2/) { print "has T\n" } DB<1> p { use re qw(Debug More); $re1=qr/g/; } Compiling REx "g" Final program: 1: EXACT <g> (3) 3: END (0) anchored "g" at 0 (checking anchored isall) minlen 1 DB<2> s main::(t.pl:12): print $_, "\n"; DB<2> beta main::(t.pl:13): if (/$re1/) { print "has P\n" } DB<2> Guessing start of match in sv for REx "g" against "beta" Did not find anchored substr "g"... Match rejected by optimizer main::(t.pl:14): if (/$re2/) { print "has T\n" } DB<2> main::(t.pl:14): if (/$re2/) { print "has T\n" } DB<2> has T main::(t.pl:12): print $_, "\n"; DB<2>

If you want to switch back and forth, then you may want another variable that you can use to hold the original (nondebug) version, so you can copy it back after you're done with the debug version.

Note: As you can see, I originally intended to leave regex debugging on by doing so after compiling all the regexes. But it seems that it was optimized out, so that's why I had to wrap it during debugging. It may be easy to leave the regex engine in debug mode so you can avoid the do { } block, but once I had something that worked, I stopped.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re^6: Turning on regexp debugging at runtime by roboticus
in thread Turning on regexp debugging at runtime by SBECK

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2020-01-22 13:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?