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

Hi All,

I know this has been discussed before, and every node seems to point to a parsing module, but I want to avoid using a module for reasons explained below I have a set of about 10 templates .. each one has different %tags% which my script replaces with variables.. currently by using s/%tag%/$value/; which works well.
But in the effort to make my code nice and clean by using -w and strict, these parsing subroutines are causing errors.
This is because several subroutines are holding variables which the parse subroutine uses, hence when the parse sub routine is run, there are undefined variables from other subs, and none are declared..

So I am looking for a good way to do this, I know there are modules for this, but I want to make my code as portable as possible without the need to use external modules

Any suggestions or good approaches to this are hugely appreciated

Many Thanks

Replies are listed 'Best First'.
Re: Parsing Templates
by grep (Monsignor) on Jan 13, 2002 at 05:11 UTC
    Reinvention of a good wheel is generally not a good idea. I have installed Template Toolkit on several systems without a hitch (*nix and M$ systems). Having to install outside modules, really does not make the system less portable (unless the modules will not install on various systems, but as I have said at least one very good templating system does). Yeah there's a little more over head to getting the system installed, but I would rather take more time in the front end than not have a product or even worse have a poorly functioning product.

    So to make a long answer short: I would recommend reevaluating the specification of no outside modules.

    grep
    grep> cd pub grep> more beer
      And i will 'third' this motion. I really think that the question itself is a testimony to USE avaible modules instead of trying to roll your own.

      Now, i can really appreciate wanting to avoid external modules to make your program more portable - but there is nothing stopping someone from packaging up those modules with the application...

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)
      
      I second it. Popular modules often are much more portable than homegrown solutions for very simple reason: there is much more people who use them on very different platforms and on different versions of Perl.

      --
      Ilya Martynov (http://martynov.org/)

Re: Parsing Templates
by dvergin (Monsignor) on Jan 13, 2002 at 13:34 UTC
    I heartily encourage you to stick with strict and -w. It can feel like a real pain at first to get used to them. It can seem like they are just getting in your way. But the warnings they produce are telling you that you have numerous latent or actual errors in the way you are doing things.

    They will catch a lot of errors for you and your work will go much better once you get the knack and you will wonder how you ever got anything done without them.

    In this case it is going to require you to rework how you make values available in your subroutines. But your code will be much more robust and easy to maintain once you get it strict/-w compliant.

    It is hardest when you have to convert a sizable chunk of non-strict/w code that (almost) works the way you want. When you start out on a new project with strict and -w things go much easier. So call it penance, bite the bullet, face the music, clean things up, and get on with life as a better programmer trusting that your pals 'use strict' and '-w' will help you avoid errors that lesser beings still struggle with.

    Programming is hard enough. We need all the help we can get.

Re: Parsing Templates
by uwevoelker (Pilgrim) on Jan 13, 2002 at 13:42 UTC
    s/%tag%/$value/g if defined $value;
    If your variables aren't declared, you could copy them to an hashref and pass this hashref to your template parser.
    $hash = {key1 => 'value', key2 => 'value'}; ... foreach my $key (keys %$hash) { s/%$key%/$hash->{$key}/eg; }
Re: Parsing Templates
by metadoktor (Hermit) on Jan 13, 2002 at 05:05 UTC
    Why don't you study the parsing modules and copy the relevant functions or modules into your own code?

    Is your program a single monolithic piece or is it composed of several modules?

    metadoktor

    "The doktor is in."