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

Re: [MacPerl] interpolating



The following might only be interesting for people who use
Text::Template a lot ...

Concerning the thread topic "interpolation" look for "sub evalit".
This is a recipe taken from the cookbook.

At 12:28 Uhr -0500 03.12.1999, Nathaniel Irons wrote:
>On 12/3/99 at 10:41 AM, rose@sj.com (Axel Rose) wrote:
>
> >   Text::Template
> > runs but can get clumsy if you have lots of backslashes (LaTeX syntax)
> > or you have a lot of different templates.
>
>Change the delimiters to something other than "{}" and the backslash
>escapes are disabled (plus you get a speed benefit, according to the
>docs).  I don't understand what you mean about lots of different
>templates -- you object to having to call them separately?

I already changed the delimiter. This disables backslash escapes
indeed but doesn't help with LaTeX syntax where you need double back-
slashes to end a line in a table e.g.. (see my code below)
The reason for this is Perls string processing. You need four backslashes
to return a double one. (my example needs 5 backslashes!)
I already had a discussion with the author Mark-Jason Dominus (very
brilliant BTW) and he explained that there is no way around this with
his module. He suggested Text::Metatext.

#!perl

use Text::Template;
my $FONT;

%templ = (
A5 => '
\begin{longtable}{lr}
<< $FONT ?
        q( \begin{futural}row1 \end{futural} & \begin{futura}) . $mystring . q( \end{futura} \\\\\ ) :
        q( \begin{times}row1 \end{times} & \begin{futura}) . $mystring . q( \end{futura} \\\\\ );
>>',
);

$FONT = 1; # set as program parameter normally
$mystring = "something";
$mytmpl = new Text::Template( TYPE => STRING, SOURCE => $templ{A5}, DELIMITERS => ['<<', '>>'] ) || die "no new templ";
print $mytmpl->fill_in(), "\n";
$FONT = 0;
$mytmpl = new Text::Template( TYPE => STRING, SOURCE => $templ{A5}, DELIMITERS => ['<<', '>>'] ) || die "no new templ";
print $mytmpl->fill_in(), "\n";

exit 0;
__END__

Now imagine you have lots of different templates. You end up with very hard
maintainable code because you can't remember whether the template contains
"if" statments or your main program cares about the program flow.

The Metatext module doesn't work on the Mac.
So what I'm using now is a OO module which works like this:

#!perl

use Text::Bundle;

my $tmpl = Text::Bundle->new( 'filename' );
$mystring = "something";
$FONT ?
	print OUT &evalit( $tmpl->get('A5') :
	print OUT &evalit( $tmpl->get('A51');

sub evalit()
{
    $_[0] =~ s/\$(\w+)/${$1}/g;
    return $_[0];
}

'filename':
@@A5@@
\begin{longtable}{lr}
\begin{futural}row1 \end{futural} & \begin{futura} $mystring \end{futura} \\
@@A51@@
\begin{longtable}{lr}
\begin{futural}row1 \end{futural} & \begin{futura} $mystring \end{futura} \\


Compare both examples. You might disagree about aesthetic but certainly
understand what I mean with "clumsy" when using Text::Template.

Text::Bundle is not on CPAN. I don't have permission to publish this work.


Axel

----------------------------------------------------------------------
Axel Rose, Springer & Jacoby Digital GmbH & Co. KG, mailto:rose@sj.com
  pub PGP key 1024/A21CB825 E0E4 BC69 E001 96E9  2EFD 86CA 9CA1 AAC5
  "... denn alles, was entsteht, ist wert, daß es zugrunde geht ..."

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org