Substituting variables in a text file 
Author Message
 Substituting variables in a text file

Here's the problem: I'd like to substitute words in a text file with their
counterpart variable in a perl script. (That was horribly phrased, but maybe
this will help explain...)

Example: the text file might look like this:  Hello $NAME.  How are you?

And let's say, for simplicity sake, that my perl file has a line like this:
$NAME = "Joe";

Of course, I have like 50 variables. Before I had to create "placeholders"
for the names like:

open (FILE, "filename.txt");
$printtemp = join ('',<FILE>);
close (FILE);
$printtemp =~ s#<!-- NAME -->#$NAME#sg;

But I really don't want to do that for ALL of them. Unfortunately, that's
all I know how to do.

Anybody have an easier way to open up a file and replace the exact variables
without having to create variable placeholders and substitute them one at a
time like I did in the above example?

-Jeff



Thu, 24 Jul 2003 07:04:50 GMT  
 Substituting variables in a text file


Quote:
> Here's the problem: I'd like to substitute words in a text file with their
> counterpart variable in a perl script. (That was horribly phrased, but maybe
> this will help explain...)

> Example: the text file might look like this:  Hello $NAME.  How are you?

> And let's say, for simplicity sake, that my perl file has a line like this:
> $NAME = "Joe";

> Of course, I have like 50 variables. Before I had to create "placeholders"
> for the names like:

> open (FILE, "filename.txt");
> $printtemp = join ('',<FILE>);
> close (FILE);
> $printtemp =~ s#<!-- NAME -->#$NAME#sg;

> But I really don't want to do that for ALL of them. Unfortunately, that's
> all I know how to do.

> Anybody have an easier way to open up a file and replace the exact variables
> without having to create variable placeholders and substitute them one at a
> time like I did in the above example?

#!/usr/local/bin/perl
use strict;
use warnings;

my $foo = 'hello $bar - are you all${baz}th $boo';

my $bar = 'world';
my $baz = 'rig';
my $boo = 'tonight?';

print $_ if ($_=$foo)=~s/(.*?)(\$[\w{]+}?)(.*?)/join'',$1,eval$2,$3/eg;

--
----------------------------------------------------------------------

Southern Division Classic Bikes - www.southern-division.com



Thu, 24 Jul 2003 07:19:04 GMT  
 Substituting variables in a text file

Quote:

> Here's the problem: I'd like to substitute words in a text file with their
> counterpart variable in a perl script. (That was horribly phrased, but maybe
> this will help explain...)
[...]
> open (FILE, "filename.txt");
> $printtemp = join ('',<FILE>);

the idiom goes

undef $/; # use a block and local() in stead if you need $/ in other places
$printtemp = <FILE>;

Quote:
> close (FILE);
> $printtemp =~ s#<!-- NAME -->#$NAME#sg;
> [...]
> Anybody have an easier way to open up a file and replace the exact variables
> without having to create variable placeholders and substitute them one at a
> time like I did in the above example?

Yeah - you _could_ look into eval (perldoc -f eval). But I like the
"placeholders" better. eval is generally viewed with a lot of suspicion
since it's generally an indicator of questionable design and can be
pretty dangerous (though of course it's called for in some cases).

I think you should use some kind of markup ($XXX or <%&XXX> or whatever)
and then use a substitution regex to extract the "keyword" and replace
the markup sequence with values that you keep in a hash. Dont let the
variable's names carry semanthics - it's bad design:

#!/usr/bin/perl -w

use strict;

my %symbols = (
               NAME => "Albert",
               DOG  => "Fido",
               CAR  => "Dodge"
               );

undef $/;
my $text = <DATA>; # this can be questioned by the way, but if the file's
                   # not huge it's OK

$text =~ s/%!([^!]+)!%/$symbols{ $1 }/g;

print $text;

__DATA__
Hello! My name is %!NAME!%, and all I ever do
is walking my dog %!DOG!%. That is unless
I'm out for a spin in my %!CAR!%.

--
Jakob Schmidt
http://aut.dk/orqwood
etc.



Thu, 24 Jul 2003 08:11:52 GMT  
 Substituting variables in a text file

Take a look at the Text::Template module.
It is doing exactly this (and much more).

Mihai



Thu, 24 Jul 2003 15:08:59 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Substituting $variable strings in a file

2. Substitute variables when reading file

3. Substitute variables when reading file

4. split and substitute, substitute, substitute

5. substituting string of text

6. Need help substituting text except when in an HTML anchor

7. methods to insert/substitute blocks of text?

8. substituting text in a var (beginner question)

9. Newbie Q: variable replacement part of substitute

10. Retreiving a template, and substituting variables

11. substituting several variables at once.

12. Newbie - using variable for search or substitute option

 

 
Powered by phpBB® Forum Software