[Prev][Next][Index][Thread]
Re: Some inquiries about Dylan tutorials and documentation
Bruce Hoult <brucehoult@pobox.com> wrote:
> In article <38D9118C.6CCBEC93@openscript.com>, James
> <james@openscript.com> wrote:
>
> > If this offer goes for anyone, I'd love to see the following Perl code
> > translated.
> >
> > [snip]
> >
> > It basically looks through an html document for Perl variables and
> > replaces them with their values as computed by perl. Pretty slick when
> > creating dynamic web pages.
>
> Can't be done -- [...] Your "${$1}" is a use of eval.
Something with an equivalent meaning can be, of course. Such as:
define method expand-template(template :: <string>,
symbol-table :: <string-table>)
=> (html :: <string>)
for (template-var :: <string> in symbol-table.key-sequence,
html = template then substring-replace(html,
template-var,
symbol-table[template-var]))
finally
html
end for;
end method expand-template;
So if the symbol-table contained the (key,value) pair ("foo", "bar"),
then calling expand-template on "foofoofoo" would return "barbarbar".
Using an explicit table of expansions is IMO better style, since
changing the variable names shouldn't affect the output of the
program. Plus when your template language grows into a full
programming language (and this always happens -- witness PHP), you'll
be in a better position to write an interpreter with comprehensible
behavior. :)
(I don't think this is the most efficient possible implementation of
expand-template, though, since the time it takes is linear in the
number of entries in symbol-table -- can anyone think of a better one
off-hand, including a suggestion to use xformat?)
Neel
Follow-Ups:
References: