in reply to Turning on regexp debugging at runtime

Out of curiosity I hacked something up for you:

{ my %orig; sub re_db_on (\$) { my ($r) = @_; die "not a regex" unless ref $$r eq 'Regexp'; return if $orig{$r}; $orig{$r} = $$r; # note this depends on the proper stringification of the regex $$r = eval qq{ use re qw/Debug EXECUTE/; qr/$$r/ }; } sub re_db_off (\$) { my ($r) = @_; return unless $orig{$r}; $$r = delete $orig{$r}; } } my $ra = qr/a/; my $rb = qr/b/; my $rc = qr/c/; my $str = "abc"; $,=", "; $\="\n"; print $str=~$ra, $str=~$rb, $str=~$rc; re_db_on $rb; print $str=~$ra, $str=~$rb, $str=~$rc; re_db_off $rb; print $str=~$ra, $str=~$rb, $str=~$rc;

Tested on v5.20 / Linux. The subs work when called from the debugger too.

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

    Even though it's noted in the comment in the code, I guess that caveat should be highlighted: This method will probably not work reliably on regular expressions that can't be completely represented by their stringified selves.