bradleyb has asked for the wisdom of the Perl Monks concerning the following question:

I am a just beginning to learn Perl - it's my first programming language actually. It's my project for the summer. So I hope this is a simple question. I am reading in a text file that has perl variables within it (ie. $name, $address, ect). - but when I print the file - it prints out the variable names (ie. $name, $address, etc). Is there a way to have the file print but having the variables replaced with the information that those variables contain - or just blank if that variable isn't defined? Thank you.

Replies are listed 'Best First'.
Re: Reading file with variables within it
by ikegami (Patriarch) on Jun 07, 2010 at 00:44 UTC
    Your file is a template. You could use String::Interpolate if you keep your existing template syntax. (Yeah, it's API is weird.) If you're willing to change your template syntax, you could switch to something that's designed to deal with templates like Template::Toolkit.
Re: Reading file with variables within it
by ww (Archbishop) on Jun 07, 2010 at 00:52 UTC
Re: Reading file with variables within it
by llancet (Friar) on Jun 07, 2010 at 01:53 UTC
    Please give a sample of your text file. Moreover, it seems you are using a unsafe way to store data. Please have a look at hash data structure, and modules like Data::Dump or YAML.
Re: Reading file with variables within it
by pemungkah (Priest) on Jun 08, 2010 at 02:00 UTC
    Of note: Text::Template does exactly this: You hand it a template and a hashful of values, and it interpolates 'em in. Handy for a quick and dirty, small templating job, but anything more than a few lines in a couple files, and I'd prefer Template::Toolkit.