Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > variable interpolation and file read

Reply
Thread Tools

variable interpolation and file read

 
 
jkatz@artsci.wustl.edu
Guest
Posts: n/a
 
      11-30-2005
Can I force variable interpolation on a file read?
I'd like the two print statements below to produce the same
results.....
(where the file myinputfile just contains the exact characters in $line
below--
without the quotes of course. )


#!/usr/bin/perl -w


$myvalue="girl";
$line= "It's a $myvalue ";
print $line."\n";

open (INPUT, "<myinputfile");
$line=<INPUT>;
print $line."\n";

Alternatively, how could I use a s/// to make the substitution?
(I want the substitution to work automatically for ANY variable I might
have
set earlier in the perl file.)

Thanks in advance for your help!

LCK

 
Reply With Quote
 
 
 
 
jkatz@artsci.wustl.edu
Guest
Posts: n/a
 
      11-30-2005
In fact efficency isn't a big issue here--the input files aren't very
big. However the contents of $line can't be known by the perl script
beforehand, only the names and values of the variables.
I think however that what you suggests implies that I should read
$line,
split on either side of $myvalue and then eval
$myvalue. Am I understanding you correctly?

LCK

 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      11-30-2005
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> In fact efficency isn't a big issue here--the input files aren't very
> big. However the contents of $line can't be known by the perl script
> beforehand, only the names and values of the variables.


Reading between the lines a bit, it sounds like what you're looking for is
a "template" library. These work sort of like the old "mad-lib" games. You
create a template with placeholders in it, and then the template library
reads that and fills in the proper values.

There are many such libraries available in CPAN- search for "template" at
<http://search.cpan.org>.

My own favorite is the venerable Text::Template, although that's largely a
matter of intertia on my part. I tried it out early on (I think it was the
first such module on CPAN), and it's never ****ed me off enough that I've
felt the need to look at alternatives.

> I think however that what you suggests implies that I should read
> $line, split on either side of $myvalue and then eval
> $myvalue. Am I understanding you correctly?


A word of advice: Before you consider taking PG's advice on *anything*, you
might want to review her posting history here.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      12-01-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Can I force variable interpolation on a file read?


No.

See FAQ: "How can I expand variables in text strings?"

However the FAQ answer is incomplete so see also numerous[1] previous
threads on the subject by seaching for the phrase "How can I expand
variables in text strings?"

[1] ~2/month for the last few years.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable interpolation and m in regular expression matching Josef Moellers Perl Misc 8 01-23-2008 12:13 PM
heredoc with variable interpolation breaks script Oliver Heins Ruby 2 09-11-2006 03:59 PM
How to stop interpolation of Escape character in variable? \Rob\ ASP .Net 8 02-24-2006 08:52 AM
Variable Interpolation with %%variable phrankster Perl Misc 9 08-14-2005 12:19 AM
Variable interpolation question Andrew Fabbro Python 3 11-17-2003 07:44 PM



Advertisments