And indeed your code doesn't work!
Yeah, I caught that a little late. As penance I tried to track down a hook, but couldn't find one. And so I offer up a solution using a guard. It's more intimidating, but at least it auto-expires and allows fine-grained localization.
use strict;
use warnings;
my $log = parse_regex( q#(\\.|["']|x)# );
print $log;
warn "And STDERR is back\n";
sub parse_regex {
my ($regex) = @_;
# Catch and stash warnings
my $guard = stderr_eater(my $parselog);
# --- compile regex
eval q{
use re 'debug';
qr/$regex/;
};
return $parselog;
}
sub stderr_eater {
package STDERR::Eater;
open my $guard, '>&', STDERR;
close STDERR;
open STDERR, ">", \$_[0];
return bless \$guard;
sub DESTROY {
close STDERR;
open STDERR, ">&", $_[0];
}
}
My first thought was YAPE::Regex. Actually my first thought was roll my own, but that's more of a reflection on my own poor choices. It looks like there's some reasonable robustness there, but was unsure if this fell into the "online analyzers which are easily fooled". It'll at least take care of the tokenizing for you.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|