[lug] Quick perl question

Rob Nagler nagler at bivio.biz
Wed Dec 11 22:05:54 MST 2002


> I need to substitute some data values for some patterns in a text template
> file and print it out.  There is only one template at a time, but multiple
> sets of data values.

This is novel. ;-)

> I was thinking I could read the whole thing into a scalar and run a series
> of pattern replacements on the scalar, but was wondering if there were
> other methods that might be better, faster, and more powerful than ever
> before.

There are a zillion template languages.  I've written several. :)
Template-Toolkit is quite popular.  However, depending on syntax, it
may be faster to do:

    $template =~ s/<(\$\w+)>/$1/eeg;

(Yes, two e's) Where the variables are loaded in variables by name,
e.g. $first_name, $last_name, etc.  Alternatively, you could:

    $template =~ s/<\$(\w+)>/$some_hash->{$1}/g;

where $some_hash is:

    my($some_hash) = {
    	first_name => "Rob",
	last_naem => "Nagler",
    };

or $some_hash could be "tied" to a dbm file or you could 

Rob





More information about the LUG mailing list