in reply to reinterpolation of regexp (and strings?)

but that is cumbersome for the user

Presuming the code will end up in a script,

perl -s -e "$data='abcdef'; $data=~s#$r#qq{$s}#ee; print $data" -- -r= +(abc) -s=$1hello

Note that the caller is able to execute arbitrary code, but that goes in hand in hand with what you asked. You'd need some kind of template system to be safe. (Upd: An anonymous monk has provided some.)

Replies are listed 'Best First'.
Re^2: reinterpolation of regexp (and strings?)
by dHarry (Abbot) on Jul 23, 2008 at 08:00 UTC

    It's the running arbitrary code part that worries me a little bit.

    However in the stuff mentioned by Anonymous Monk the use re 'eval' pragma is being used. I guess in God we trust;-)

      I don't see use re 'eval'; in String::Interpolate, for starters.

      And there's nothing wrong in use re 'eval'; per say. It's sometimes required to execute dynamically built regexps. I've used it a few times, and none of them involved executing untrusted code.

        I don't see use re 'eval'; in String::Interpolate, for starters.

        I meant Regexp::NamedCaptures, my mistake.

        And there's nothing wrong in use re 'eval'; per say...

        If you construct the regexps yourself fine, but If you ask the user to supply the code it can be dangerous.

        I don't see use re 'eval'; in String::Interpolate, for starters.

        You're right of course. I meant Regexp::NamedCaptures. My mistake.

        And there's nothing wrong in use re 'eval'; per say. It's ….

        If you construct the regexps yourself based on some logic fine, if some user supplies the code (as stated) it can be risky.