Substituting $variable strings in a file 
Author Message
 Substituting $variable strings in a file

I want to embed variable names in a file
then read the file and have the variables
interpreted.  Below is the shell of the
code, I've tried escapes, eval, and read
all docs I could think of ... I DON'T
want to do it with S/UBSTI/TUITION or with
 < <HERE.  Basically I'm thinking the program
is compiled and there's no way to get it
to interpret the $word in the file?

#!/usr/local/bin/perl -w

use strict;
use diagnostics;

my $word = "stinky";

## I actually want to read from a file

my $string =   <DATA> ;

## I want to interpret variables stored in file

print $string;

__END__
this is my $word program

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Substituting $variable strings in a file

Quote:

> I want to embed variable names in a file
> then read the file and have the variables
> interpreted.

This is a FAQ.  Have you looked there?


Wed, 18 Jun 1902 08:00:00 GMT  
 Substituting $variable strings in a file

Quote:

> I want to embed variable names in a file
> then read the file and have the variables
> interpreted.  Below is the shell of the
> code, I've tried escapes, eval, and read
> all docs I could think of ... I DON'T
> want to do it with S/UBSTI/TUITION or with
>  < <HERE.  Basically I'm thinking the program
> is compiled and there's no way to get it
> to interpret the $word in the file?

As already mentioned, there is a FAQ for this...

Depending on what can be in the file, this can certainly be tricky.
You need to quote the data twice -- once for the eval, and once so
that it will be treated as a double-quoted string inside the eval.
The problem is that the second quoting mechanism needs to be something
that will not collide with the data.

For example, if the data contains a ", then using:

print eval qq("$string");

will not work as the embedded " will cause a syntax error.  You can
reverse it:

print eval "qq($string)";

which will handle embedded "s fine, but will break on embedded ")".
The normal way to get around this problem is with a HERE document, but
you specifically disallowed that.  I'm going to assume that you didn't
really mean to disallow it in this circumstance, so here it is:

print eval qq(<<STRING_NOT_IN_THE_DATA
$string
STRING_NOT_IN_THE_DATA);

Watch out for the newline that gets included at the end with this
method.  Also note that STRING_NOT_IN_THE_DATA *can* be in the data as
long as it isn't on a line by itself.

--
Ren Maddox



Wed, 18 Jun 1902 08:00:00 GMT  
 Substituting $variable strings in a file

Quote:


> > I want to embed variable names in a file
> > then read the file and have the variables
> > interpreted.

> This is a FAQ.  Have you looked there?

Not only is it a FAQ, but is asked at least once a week.  The answer
in the FAQ is arguably actually not the best answer so it pays to read
the last half-dozen threads on this question too.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Variables in Strings and Substituting a Path Name

2. Substituting variables in a string?

3. Substituting variables in a text file

4. Substitute variables when reading file

5. Substitute variables when reading file

6. Substituting many sparsely distrbuted strings in many files

7. how to substitute strings in a newly created file

8. substituting strings in many files

9. Substituting Strings in Many Files

10. Substituting strings (with newlines) in many files

11. split and substitute, substitute, substitute

12. Replace a String in a File with an other String both in variables

 

 
Powered by phpBB® Forum Software