Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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'

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002575]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2017-11-21 14:26 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (302 votes). Check out past polls.