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

I am using templates to manage a couple web sites. Right now I am doing something simular to this:
open(FILE,$template); foreach (<FILE>) { if ($_ =~ m/MY_MATCH/) { &do_something; } else { print $_; } } close(FILE);

Now this works fine, except, it requires the pattern that is being matched to be on a line by itself, which isn't always the case after our webmaster mangles it with frontpage or dreamweaver.

What is the best way to match this, so that it can be replaced anywhere?

Replies are listed 'Best First'.
Re: using templates
by LTjake (Prior) on Oct 07, 2002 at 12:31 UTC
    My suggestion would be to avoid rolling your own in this situation. There are lots of great pre-existing modules to do templates (ex: HTML::Template, Template::Toolkit). They provide the simple "replace" functionality that you're trying to implement as well as much fancier things (loops, for instance) which you can use to further enhance your documents

    See this article to find out which one is best for you.

    My own personal preference is to use HTML::Template because of its simplicity.

    Here's a quick example (given here) using HTML::Template:

    A template file (test.tmpl)
    <HTML> <HEAD> <TITLE>Test Template</TITLE> </HEAD> <BODY> My Home Directory is <TMPL_VAR NAME="home">. My Path is set to <TMPL_VAR NAME="path">. </BODY> </HTML>
    ...and some code to use the template:
    use HTML::Template; # open the HTML template my $template = HTML::Template->new(filename => 'test.tmpl'); # fill in some parameters in the template $template->param(home => $ENV{HOME}); $template->param(path => $ENV{PATH}); # send the obligatory Content-Type print "Content-Type: text/html\n\n"; # print the template print $template->output;
    Update: Fixed Template::Toolkit link, thanks zigdon.
Re: using templates
by zigdon (Deacon) on Oct 07, 2002 at 12:29 UTC

    One option is to use one of the wonderful templateing modules, like HTML::Template. I switched to that after years of rolling out my own templates, and I've never looked back.

    However, if you can't use a module, or perhaps don't want to, you could do something like this:

    # It's good practice to make sure your open succeeds open(FILE,$template) or die "Can't read template: $!"; # while instead of foreach, since while (since it's # scalar context) reads the FILE a line at a time, instead # of slurping it all into memory while (<FILE>) { # replace only your match with the return value of the sub s/MY_MATCH/&do_something/eg; print; } close FILE;

    -- Dan

Re: using templates
by joe++ (Friar) on Oct 07, 2002 at 12:29 UTC
    Two quick hints:

    1. Search CPAN for Template - this returns a bazillion hits for Text::Template, HTML::Template, Apache::Template and many, many more...

    2. Refrain from using Perl and use XML/XSL(T) whenever appropriate ;-)

    Update:As ajt says correctly, "you don't need to refrain from Perl to use XML/XSL-T, Perl is good at that too! Try LibXML and LibXSLT". Very true and - in fact - I use these as well!

    HTH!

    --
    Cheers, Joe