interpolation

N

new2perl

Assigning a variable.
Then reading in text file that uses the variable.
It's isn't resolved.

Input file:
Your name is: $name

Script:
#!/bin/perl

$name="Rupert";
$text="textin";

open (MYFILE, $text) or die "cant open erik";

while (<MYFILE>) { $value = $_ }

print "$value";


Output:
Your name is: $name


Output that I want:
Your name is Rupert.


What am I doing wrong here ?

Bob
 
T

Tad McClellan

Assigning a variable.
Then reading in text file that uses the variable.
It's isn't resolved.


Of course not. Data is not the same thing as Code.

Input file:
Your name is: $name

Script:
#!/bin/perl

use strict;
use warnings;

$name="Rupert";

my $name="Rupert";

$text="textin";

open (MYFILE, $text) or die "cant open erik";


You did not try to open "erik", you tried to open "textin".

while (<MYFILE>) { $value = $_ }


That reads all the lines, and discards all of them except the last line.

print "$value";


print $value;

perldoc -q vars

What’s wrong with always quoting "$vars"?

Output:
Your name is: $name


Output that I want:
Your name is Rupert.


What am I doing wrong here ?


Conflating what is "data" with what is "code".


Your Question is Asked Frequently:

perldoc -q expand

How can I expand variables in text strings?


However, I'm quite sure that you are asking the wrong question.

You should instead be asking about Templating systems.
 
N

new2perl

Of course not. Data is not the same thing as Code.



use strict;
use warnings;


my $name="Rupert";



You did not try to open "erik", you tried to open "textin".


That reads all the lines, and discards all of them except the last line.


print $value;

perldoc -q vars

What's wrong with always quoting "$vars"?




Conflating what is "data" with what is "code".

Your Question is Asked Frequently:

perldoc -q expand

How can I expand variables in text strings?

However, I'm quite sure that you are asking the wrong question.

You should instead be asking about Templating systems.

this should do it:
$value =~ s/\$(\w+)/${$1}/g;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top