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

Re: Turning on regexp debugging at runtime

by Anonymous Monk
on Sep 23, 2014 at 15:12 UTC ( #1101663=note: print w/replies, xml ) Need Help??


in reply to Turning on regexp debugging at runtime

As of 5.9.5 the directive use re 'debug'; is lexically scoped, so you can do something like:

my $re1 = qr/foo|bar/; { use re 'debug'; my $re2 = qr/quz|baz/; { no re 'debug'; my $re3 = qr/hello|world/; } }

And you'll only get debugging info for the scope where the directive is on.

See 'debug' in re and the pages it links to for more details. As you can see in that documentation, you can also use use re qw(Debug EXECUTE); to get only the debug output related to the run-time phase.

Replies are listed 'Best First'.
Re^2: Turning on regexp debugging at runtime
by Anonymous Monk on Sep 23, 2014 at 15:28 UTC

    As for easily turning debugging on and off, here's one way via environment vars:

    my $re1 = qr/foo|bar/; { use if $ENV{DEBUG_REGEX}, qw/re Debug EXECUTE/; my $re2 = qr/quz|baz/; print "xfoox"=~$re1, "ybazy"=~$re2; }

    gives you run-time debug info on $re2 only when the environment var DEBUG_REGEX is set.

      Thanks for both of these replies, especially the second since I wasn't aware of the DEBUG_REGEX environment variable.

      I should have been a bit clearer, but it appears that what I want to do probably can't be done. For example, say I have the absurdly simple script:

      $re1 = qr/a/; $re2 = qr/b/; $re3 = qr/c/; $str = "ab"; $str =~ $re1; $str =~ $re2; $str =~ $re3;

      what I want to do is step through the code (in the debugger) and turn on debugging (by executing "use re 'debug'") when I match against $re2 but then turn it back off before proceeding to the $re3 match.

      I could put each in it's own scope, but that would be messy (I don't really want to put every regexp in it's own scope so that I could potentially debug it individually).

      Even worse is that the regexps in question were built outside of the scope, so I actually end up with something more similar to this:

      $str = "abc"; $re1 = qr/a/; $re2 = qr/b/; { use re 'debug'; $str =~ $re1; { no re 'debug'; $str =~ $re2; } }

      which won't print any debugging info at all.

      Oh well, I can do what I've always done (temporarily enable debugging and then go to the point in the code I'm interested in ignoring all the unwanted debugging output).

        The name DEBUG_REGEX is made up, and can be anything (note the test of $ENV{DEBUG_REGEX} in the code).

        How about:

        my $re1 = qr/a/; my $re2 = do { use if $ENV{FOO_DEBUG_REGEX}, qw/re Debug EXECUTE/; qr/b/ }; my $re3 = qr/c/; my $str = "ab"; $str =~ $re1; $str =~ $re2; $str =~ $re3;

        will output run-time debug info on $re2 only when the environment var FOO_DEBUG_REGEX is set.

        I'm not sure if this can be done dynamically in the debugger, perhaps another monk with more experience there can help. The documentation does say that use re 'debug'; has both compile-time and run-time effects.

        Hello

        Are you aware you can scope more easily using

        $str = "abc"; $re1 = qr/a/; $re2 = qr/b/; { use re 'debug'; $str =~ $re1; } $str =~ $re2;
        However since the whole process does not seem to help you anyway: Can you please provide more code? It doesn't seem very useful to match like crazy but not doing anything with the match results.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2019-10-23 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?