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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|