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

logic within interpolation

by karrakis (Novice)
on Mar 29, 2013 at 21:29 UTC ( #1026193=perlquestion: print w/replies, xml ) Need Help??
karrakis has asked for the wisdom of the Perl Monks concerning the following question:

The details of why I need to do this are somewhat sketchy but the contents of the examples should make it clear that what I'm doing is legal and not intended to destroy someone *else's* poorly protected unsanitized eval. At a certain point in the running of a specific algorithm, an eval call is made. That eval looks like this:
$outputarray->[$level] = eval "\"$interpol\"";
I want the output to be dependent on the contents of a regex-captured variable $1. This variable will contain a number (as a string) either of one digit, or more than one digit. I need to make a conditional statement that is evaluated within quotes, so I was hoping to use ${ } as a block instead of a variable call. something like:
${if($1 =~ /\d{2,}/){return "driving a vehicle ${1} lbs over the weigh +t limit";}elsif($1 =~ /\d{1}/){return "driving a vehicle $1 tons over + the weight limit";}}
except that, clearly, if this worked, I wouldn't be here. The logic of this algorithm is separate from the parser that contains the eval. I cannot commingle the two. Due to the constraints of the system, my only option for basing a condition on this temporary variable is to feed that logic through this eval.

I'm not asking whether this is the right way to do it, it seems pretty clear it isn't. I'm just asking if it's *possible* ^_^ (and no, I can't change the structure of the eval. The \"s stay right where they are. The only thing I can modify, at least for the purposes of this experiment, is the string contained by $interpol).

Any thoughts?

Replies are listed 'Best First'.
Re: logic within interpolation
by Anonymous Monk on Mar 29, 2013 at 21:35 UTC
      the problem actually lies in the 'nesting' of the interpolations being done.

      ${if(this){return 'this';}} is fine, because the single quotes do not result in this during eval:

      eval ""this""; <- problem.

      eval "'this'"; <- evaluated properly.

      So the problem is I need to find a way to interpolate the contents of $1 and *then* have those contents interpolated again. I think I'm on the right track now, but thanks for the boilerplate, some of that looks to be useful reading.

        do you want to do this?

        eval "qq{$interpol}"

      You must have a copy & paste buffer dedicated to this answer... ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026193]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-06-18 01:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.