Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: "eval" variable in non-perl statment?

by athomason (Curate)
on Mar 29, 2001 at 00:38 UTC ( #67938=note: print w/replies, xml ) Need Help??

in reply to "eval" variable in non-perl statment?

Superior methods exist for what you want to do (see other posts and the caveat at the end of this one), but the fix for what you have is simple. You need perl to consider your $input as a string to get it to interpolate. This code should work for you:
use strict; my $v1="user_value1"; open(INFILE,"<template.file") or die "Couldn't open file: $!"; my $input=<INFILE>; close(INFILE); $input = '"' . $input . '"';; my $line = eval $input; print "$line\n";
Depending on your desired output, you might also want to chomp off the newline that comes with $input

Also, be exceedingly careful with how you use this. You typically shouldn't eval anything passed from the user, as you're possibly introducing an enormous security headache. String eval on user data is frequently an open invitation for the user to execute code of their choice. I'd sleep easier at night using the s/// operator to fill in the values if that's an option. If you can't do that for whatever reason, use taint checking and thoroughly consider how your script could be abused.

Replies are listed 'Best First'.
Re: Re: "eval" variable in non-perl statment?
by ftforger (Sexton) on Mar 29, 2001 at 00:50 UTC
    Thanks, and I understand the security issues here. Its not really a case of evaling what the user has input as it is just expanding a variable in a template context. We will probably use the Text::Template module. The template file is to be kept inside of a configuration managed directory with controlled access, so it would be pretty hard to put something damaging in there, and the perp would leave tracks in the config management system.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2023-06-05 03:40 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (22 votes). Check out past polls.