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

Re: [MacPerl] reading variables from files



At 07:25 96-03-15, Jim Kateley wrote:

>1) Can I do what I want to do? The reason I want to do this is I'm creating a
>form where the people using it need to edit the text, but variables I'm
>pulling from a database are sprinkled throughout the text. I don't want them
>to have to edit the script.

Yes, but you need to eval the resulting string to get the extra level
of variable interpolation.

>2) Is there an easier way to read a glob of text into a variable?

If you mean, avoid the foreach loop, yes.

Here's an approach (untested):
    $myVar = "Hi There";
    $myFile = "HD:inputfile.txt";
    open (MYINPUT, $myFile) || die "Can't open file: $myFile\n";
    undef $/; # read whole file at once
    $source = <MYINPUT>;
    close (MYINPUT);
    $Whattoprint = eval( "\"$source\"" );
    die( "eval failed: '$@'\n" ) if $@;
    print $Whattoprint;

Note that all dollar signs ('$') and double quotes ('"') in the input
text need to be escaped with a backslash first.  Except, of course, for
the dollar signs on the variables to be interpolated.  E.g.:
    $myVar costs \$.02, \$1.00 to be quoted as \"$myVar\"

--Hal
Hal Wine <hal@dtor.com>     voice: 510/482-0597