CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
interpolating variables in a string from a file

 



andrew_b
stranger

Dec 1, 2005, 10:46 AM

Post #1 of 22 (2255 views)
interpolating variables in a string from a file Can't Post

Why won't this DWIM?


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

my $var = 'foo';
undef $/;
my $data = <DATA>;
$data = eval {$data};
print $data;


__DATA__
hello $var



andrew_b
stranger

Dec 2, 2005, 5:10 AM

Post #2 of 22 (2247 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post

FWIW I found an answer in 'perldoc -q expand'


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

my $var = 'world';

undef $/;
my $data = <DATA>;

$data =~ s/(\$\w+)/$1/eeg;

print $data;

__DATA__
hello $var



MrPJ
User

Dec 3, 2005, 5:46 AM

Post #3 of 22 (2241 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post

I'm always wary when I see people trying to use strings as variable names etc


andrew_b
stranger

Dec 3, 2005, 7:11 AM

Post #4 of 22 (2238 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post

Really? Most Perl programmers interpolate variable names in strings all the time...thus the whole point of Perl's excellent quoting scheme.

The question I raise is just one of the infrequent examples of a string that Perl thinks of as being single quoted, while the programmer wants it thought of as double quoted.


MrPJ
User

Dec 5, 2005, 2:34 AM

Post #5 of 22 (2230 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post

Of course people use variables within strings, but you are doing it in a round about way which I don't see a need for.


andrew_b
stranger

Dec 5, 2005, 4:23 AM

Post #6 of 22 (2228 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post

Well, keep at it. As you gain more experience you'll see a need for all kinds of things. :-)


MrPJ
User

Dec 5, 2005, 8:40 AM

Post #7 of 22 (2224 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post

Just because I question your logic shouldn't mean you have to resort to childish personal remarks (which luckily I take with a pinch of salt as I'm not the one asking questions in the beginners forum).

The point still remains that I think your logic in this case needs re-examining. Why not just use sprintf or something similar?


Code
my $var = 'numb nuts'; 
my $data = join '', <DATA>;
my $string = sprintf($data, $var);

__DATA__
Hello %s



(This post was edited by MrPJ on Dec 5, 2005, 8:41 AM)


andrew_b
stranger

Dec 5, 2005, 10:22 AM

Post #8 of 22 (2219 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post


Quote
Just because I question your logic shouldn't mean you have to resort to childish personal remarks

Note the ':-)' after my comment. Typically signifying a joke...besides, my dad can beat up your dad.



Quote
I'm not the one asking questions in the beginners forum

Ouch. - You're too smart for that aye? Well, I'm not. Besides, in the 5 years I've been visiting this forum, the beginner forum has always been the most active. Plus there are a bunch of smart cookies who frequent this place...present company excluded :-) <-- (joking again)



Quote
The point still remains that I think your logic in this case needs re-examining.

Yes, that flawed logic of interpolating variables. THE HORROR! :-) <-- (more (bad) joking)



Quote
Why not just use...

Well, for one thing, you needed to modify the data in the file. I may not have that luxury.

Thanks for your help though. :-)


MrPJ
User

Dec 5, 2005, 11:14 AM

Post #9 of 22 (2216 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post


Quote
Yes, that flawed logic of interpolating variables. THE HORROR! :-) <-- (more (bad) joking)


I didn't say it was flawed, just that there appear to be better ways of doing the same job.


andrew_b
stranger

Dec 5, 2005, 11:49 AM

Post #10 of 22 (2211 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post

I'm all ears.

In this case my 'job' was to interpolate or 'expand' variables in a string contained in a file. Perdoc gives some advice on how to do this and I took it: 'perldoc -q expand'.

To which you said:

Quote
I'm always wary when I see people trying to use strings as variable names etc


Now I assumed you were confused here because I was not 'trying to use strings as variable names' (as in symbolic references) which you probably heard were 'bad' once. So I made it clear that I'm just trying to interpolate what Perl thinks of as a single quoted string. Personally I'll stick with perldoc, but tell me, what are the 'better ways of doing the same job'?


MrPJ
User

Dec 5, 2005, 12:59 PM

Post #11 of 22 (2201 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post


Quote
In this case my 'job' was to interpolate or 'expand' variables in a string contained in a file.


Indeed, and as the variable names are in a string within a file they are themselves strings. They may look like variables but that doesn't mean they are.


andrew_b
stranger

Dec 5, 2005, 3:54 PM

Post #12 of 22 (2193 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post

Sorry, I've been making some assumptions about your comprehension. 'Variable Interpolation' might sound confusing but it's really a simple concept...I can walk you through it (or any good beginner Perl book should cover it). It's really quite similar to Shell programming. See, within single quotes, no characters are interpreted. Within double quotes, some interpretations are performed. For example, variables can be used within a double quoted string, but won't be interpreted within a single quoted string. (There are lots of useful alternative quoting mechanisms. See perdoc perlop for more.)

Now, once in a while...not very often mind you, but once in a while, you may run into a situation where you have a single quoted string (or a string that Perl interprets as single quoted) where you *want* to have Perl preform variable interpolation. (That's what this thread is about.) How can you say to Perl, "Hey Perl...treat this single quoted string like it was double quoted".

See...not so scary. As for the answer, one of the perldoc manpages (perlfaq4) tells us exactly how to do this (which I should have consulted before I started this ridiculous thread). It's actually a snap.

So now you know! btw - Now next time you hear someone say, 'Be wary of using variables in strings' or 'There are better ways to do that', but not be able to tell you *why* (or provide any workable alternative) you'll know that they are just full of crap!

hth


MrPJ
User

Dec 5, 2005, 4:48 PM

Post #13 of 22 (2192 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post


Quote
'Variable Interpolation' might sound confusing but it's really a simple concept...I can walk you through it (or any good beginner Perl book should cover it).


I have 8 years perl experience which includes commercial applications. If I'm barking up the wrong tree then forgive me - perhaps you need to explain more clearly next time.

Where I'm falling down is that you say you are reading your string from a file. Just because the string is littered with $this and $that doesn't mean you are trying to interpolate variables because $this and $that are not even variables at the point of being read from the file, they are simply strings that *look* like variables but perl knows nothing of them until you define them within your script and then reference them symolically.


Quote
Now, once in a while...not very often mind you, but once in a while, you may run into a situation where you have a single quoted string (or a string that Perl interprets as single quoted) where you *want* to have Perl preform variable interpolation.


Right, and this doesn't appear to be one of them. You are reading a string from a file. It makes no difference whether it is single or double quoted as perl has no idea that there are variables within it unless you tell it so (which is what your discovered solution does).


Quote
So now you know! btw - Now next time you hear someone say, 'Be wary of using variables in strings' or 'There are better ways to do that', but not be able to tell you *why* (or provide any workable alternative) you'll know that they are just full of crap!


I did show you an example and no not full of crap, just trying to have a conversation with an egotistical ignoramus.


KevinR
Veteran


Dec 5, 2005, 5:49 PM

Post #14 of 22 (2185 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post


In Reply To
I'm always wary when I see people trying to use strings as variable names etc


Yea, I agree. That's pretty much a sign they are trying to do something better done a different way.
-------------------------------------------------


andrew_b
stranger

Dec 5, 2005, 6:22 PM

Post #15 of 22 (2183 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post


Quote
I have 8 years perl experience which includes commercial applications.

Hey, I've been playing guitar longer than that and I suck eggs.


Quote
perhaps you need to explain more clearly next time.

OK, here's the code above with comments (note, this little example was obviously just an, err...example)


Code
#!/usr/bin/perl 
use warnings;

#make sure we can't use symbolic refs :-)
use strict;

#here is where we assign a value to $var or 'define' it as you say
my $var = 'world';

#read in the contents of the file and store it *as a string* in $data
#note, I only use the DATA filehandle for simplicity,
#could be any file
undef $/;
my $data = <DATA>;

Note that Perl views the string in $data as *single quoted* i.e. no interpolation took place. However, if the contents of the file had been enclosed in double quotes, interpolation *would* have taken place.

Code
#originally I thought an eval would cause interpolation 
#but it doesn't in this case
#however, match and substitute *do* interpolate,
#thus replacing the scalars with themselves, we get interpolation
$data =~ s/(\$\w+)/$1/eeg;

#prints hello world
print $data;

__DATA__
hello $var


Hopefully this makes sense now. It's really all about interpolating a string, albeit a single quoted one. In practice it doesn't matter that the string came from a file, it could be from anywhere.


Quote
just trying to have a conversation with an egotistical ignoramus.

Finally! Something I agree with! :-)


MrPJ
User

Dec 6, 2005, 7:34 AM

Post #16 of 22 (2169 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post

After doing some experimentation regarding this problem I'm now more than ever sure that what I'm saying is correct so either you are misunderstanding what I'm saying or I'm not explaining myself very well.

Your subject line talks about loading the string from a file but your demo uses __DATA__. I can appreciate that you might be confused about interpolation when using __DATA__ however when loading from a file it is not a question of interpolation but rather a confusion between what is and is not a variable.

The "variables" within your string in the file might look like:

This is a $string

....however $string is not a perl variable, it is simply a string that looks like a variable. It doesn't become a variable until you define $string inside your perl script and then perform the regex that matches strings like $foo and $bar and replaces them with the REAL variable you have defined in your script.

I'm sorry if you can't understand what I'm explaining.


(This post was edited by MrPJ on Dec 6, 2005, 9:01 AM)


KevinR
Veteran


Dec 6, 2005, 10:36 AM

Post #17 of 22 (2162 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post

I'm thinking andrew might be wanting to use a template, in which case there are already modules for that but for simple templates using variable substitution (instead of variable interpolation) works fine.
-------------------------------------------------


MrPJ
User

Dec 6, 2005, 10:40 AM

Post #18 of 22 (2160 views)
Re: [KevinR] interpolating variables in a string from a file [In reply to] Can't Post

I agree. It is a simple substitution with an evaluation and is not to do with interpolation. That's what I am trying and failing to get across.


andrew_b
stranger

Dec 6, 2005, 10:51 AM

Post #19 of 22 (2159 views)
Re: [MrPJ] interpolating variables in a string from a file [In reply to] Can't Post


Quote
After doing some experimentation regarding this problem I'm now more than ever sure that what I'm saying is correct ...Your subject line talks about loading the string from a file but your demo uses __DATA__.


Doesn't matter...same behavior. But then I said that above. DATA is a predefined filehandle in Perl. I could have read from any filehandle. For that matter I could have just typed the string in the script. The fact that it was even read from a filehandle is almost irrelevant. Observe:


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

my $var = 'world'; #hopefully no questions here

my $string = 'hello $var';#just a plain old string. agreed?

undef $/;
my $data = <DATA>;

#Q: what's the difference between what's in $data and $string now?
#A: Nothing!

#oops, I really wanted interpolation on that single quoted string
#I'll follow Tom Christiansen's advice from perlfaq4:
$data =~ s/(\$\w+)/$1/eeg;

print $data;
#woopie!! I'm done!!

$string =~ s/(\$\w+)/$1/eeg;
print $string; #same thing

__DATA__
hello $var


Just plain ol' interpolation.


andrew_b
stranger

Dec 6, 2005, 10:55 AM

Post #20 of 22 (2158 views)
Re: [KevinR] interpolating variables in a string from a file [In reply to] Can't Post


In Reply To
I'm thinking andrew might be wanting to use a template


Agreed! but I can't in this case for reasons that are not relevant.


In Reply To
but for simple templates using variable substitution (instead of variable interpolation) works fine.


No, it actually *is* interpolating, that's what's so cool about it! Match and substitute operators *interpolate*, that's why this works here. Otherwise it would just substitute the string $var (trust me, I looked it up :-)


KevinR
Veteran


Dec 6, 2005, 11:28 AM

Post #21 of 22 (2154 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post

yes, it is variable interpolation - the hard way. But sometimes the hard way is the only way. What is the point of all this anyway? What are you doing?
-------------------------------------------------


MrPJ
User

Dec 6, 2005, 12:34 PM

Post #22 of 22 (2148 views)
Re: [andrew_b] interpolating variables in a string from a file [In reply to] Can't Post

I see where you are coming from now with regards to interpolation. I was looking at things from a different perspective...perhaps because subconsciously I knew that reading data from a filehandle didn't interpolate so that's why I kept saying interpolation was not the issue.

I will eat humble pie and apologise for my ignorance Pirate

If interpolation was performed by default on file data then you'd end up with lots of headaches, which I guess is why it isn't done.

One concern with your approach especially when using the strict pragma is that if your file happens to contain a variable name that you haven't defined, your script will die.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives