in reply to Overriding Subroutines in Package

If you allow Perl, you allow mischief. There's really no way around it.

That's why packages like Template Toolkit have a "mini language" that allows most of what you need in a template without having to expose raw Perl.

Consider that instead of writing Yet Another Templating Language, please!

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Overriding Subroutines in Package
by Tetramin (Sexton) on Oct 07, 2001 at 19:40 UTC
    I know Mason and like it very much. Among the mini template tools I like the plain s/\$(\w+)/$values->{$1}/eg; most. So I combine both and it lets me write

    <html> <body><h1>Hello $world</h1></body> </html> <% BEGIN{ $world = "World"; } %>

    Actually no new template language, only a recombination. I wonder if I can override subroutines of Mason, too. Hopefully not.

      In Template, that's
      [% world = "World" -%] <html> <body><h1>Hello $world</h1></body> </html>
      And you don't have to expose Perl. Template is good. use Template!

      -- Randal L. Schwartz, Perl hacker

        For my script I am now using local to protect the system from being overridden under mod_perl. I don't really know if it's the right way, it's only the result of trial and failure.

        #!/usr/bin/perl use strict; use HSP; if ($ENV{MOD_PERL}) { my @keys = keys %HSP::; my $do = ''; foreach(@keys){ $do .= "local *HSP::$_ = *HSP::$_;"; } $do .= 'HSP::handle();'; eval $do; } else { HSP::handle(); }

        In fact, Toolkit, which you mentioned, has an option for variable interpolation, I just found that line that says it in the documentation. It's quite helpful when coding a template with an ASP capable editor. The code blocks appear in clickable boxes and the variables can be entered in the visible HTML. Only TT2 does use square brackets [% %] by default which should be changed to <% %> then.
        However just for my personal pleasure I would like to put perl within the brackets so I can have everything in one place. I have long enough put everything in the cgi-bin and started with #!/usr/bin/perl, I need a change!! Playing with this saves me from switching to php.