[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] reading variables from files



>What I want to be able to do is have a text file that contains:
>
>"I'm going to print out the var $myVar\n"
>
>Read it into my script, and when I print it from the script have it subsitute
>the variable.

Here's what I did:

In the text file that I was reading in, I put double percent signs around
anything I wanted to quote, like this:

...You have selected option number %%$option_number%%, and therefore I'm
going to have to shoot you...

Then, in my Perl script, as I read through my text file, I do

  s/%%(.*?)%%/$1/gee;   # the .*? is the non-greedy pattern matcher
                        # notice the double 'e' flag at the end--you need it!

This line strips off the double percent signs, and substitutes the
evaluated variable for whatever was between the double percents.  The nice
thing about this approach is that I can do

...your service provider is %%$host%%, which is much cheaper than Compu$erve...

without worrying about accidental evaluation.