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

I'm writing some code that generates HTML pages from templates. I'd been prototyping the system with here documents, when I got the wacky idea that I might put the here doc text in a file, slurp it into a string, and have a cheap template. I probably will go with one of the excellent \w+::Template modules, but first I want to play with the idea of "templates on the cheap."

What I want, basically, is double-quote interpolation of variable contents, as the text files I have contain lines of the form

<a href='mailto: $maintainer'>Send email</a> to site maintainer.

my idea so far:

use strict; my $maintainer = 'root@god.com'; # I've also tried making this a globa +l, so I don't think it's scoping my $data; { local $/; open FILE, "filename" or die "ha ha! :$!\n"; $data = <FILE>; close FILE; }

But that leaves $data</a> as is (i.e. with a literal <code>$maintainer in it.

Out of (at this point) pure curiosity, how would I get the value of $maintainer to interpolate in there? (I've tried variations on eval and here-doc quoting, including the too-good-to-have-worked-anyhow $data = "$data"; with no sucess).

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
Re: Templates on the cheap?
by merlyn (Sage) on Nov 29, 2000 at 01:33 UTC
Re: Templates on the cheap?
by elusion (Curate) on Nov 29, 2000 at 04:02 UTC
    It may also be of interest for you to visit this node - Interpolating from Text File.

    - p u n k k i d
    "Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

Re: Templates on the cheap?
by dws (Chancellor) on Nov 29, 2000 at 06:21 UTC
Re: Templates on the cheap?
by chipmunk (Parson) on Nov 29, 2000 at 01:36 UTC
    Assuming your variable names are well-formed, this is one possibility: s/\$(\w+)/"\$$1"/gee; A preferable solution (that was suggested recently in another thread) is to use a hash instead of named variables:
    $vals{maintainer} = 'root@god.com'; s/\$(\w+)/$vals{$1}/g;
    I would lean towards using one of the Template modules, though, for robustness.