in reply to Putting the stringified Regexp object into code
There is only one edge case that springs to mind - where there are pragma that effect how the regexp works. Certain pragma like use re '/u' will affect the stringification of the regexp reference, so you don't need to worry about them. But others, like re::engine::Lua effect the behaviour of the regexp without effecting the stringification.
If you can wrap the regexp in a coderef, then you can use B::Deparse to get a string of Perl code, and that will preserve pragmata...
sub regexp2code (&) { require B::Deparse; my $re = shift; '(do ' . B::Deparse->new->coderef2text($re) . ')'; } LUA_STYLE: { use re::engine::Lua; CORE::say regexp2code { qr(%a) }; } PERL_STYLE: { CORE::say regexp2code { qr(%a) }; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Putting the stringified Regexp object into code
by sedusedan (Pilgrim) on Nov 07, 2012 at 00:18 UTC |