Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Putting the stringified Regexp object into code

by tobyink (Abbot)
on Nov 06, 2012 at 20:35 UTC ( #1002575=note: print w/ replies, xml ) Need Help??


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) }; }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Putting the stringified Regexp object into code
Select or Download Code
Re^2: Putting the stringified Regexp object into code
by sedusedan (Monk) on Nov 07, 2012 at 00:18 UTC
    Thanks, Toby. I'm bookmarking this in case I need it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2015-07-03 04:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (48 votes), past polls