wacky idea

gav on 2002-09-12T03:41:15

Has anyone ever though of using Template Toolkit to write perl scripts?

I have a set of scripts for different clients that share perhaps 50% common code. Instead of refactoring and moving the shared code to modules I had a crazy idea about using a set of templates to write out the scripts, with the template filling in the shared bits.

My original idea ran something along these lines:

  • From parameters determine what client is calling script
  • Pull rule set from database, do transforms on input
  • Create query, run against database
  • Push results through client's template

The code generation approach would have a separate script for each client (all running nicely under Apache::Registry of course) generated by the template and updated (rarely) when the client's data changes. In this case there wouldn't be a script and a template, just the script itself.

This seems an interesting route to follow, though it seems a bit wacky to me. I'll have to think about it a little further and see if I can come up with an example or two. I'm sure there are some big disadvantages which I haven't considered yet :)

Any thoughts?


Templated Perl Scripts?

ziggy on 2002-09-12T04:07:58

Any thoughts?
Yes. It's late. Get some sleep before you start hacking. :-)

If you've got a lot of common code that's mostly similar except for a few parameters, that's a sign you should centralize your common code in a module, not a template to simplify cargo cult coding (dynamically generated or otherwise). Your first instincts were correct. ;-)

There are instances where template-generated Perl scripts would be useful. I've thought about hacking/replacing h2xs so it becomes a tool to start common projects -- I want a new mod_perl handler, a new CGI program, a new command line utility, a new SAX handler, .... and I don't want the static boilerplate (by A. U. Thor ...)

Ergh

chromatic on 2002-09-12T07:11:38

Another benefit to modules is that you can pre-load them when Apache starts. This has the potential to save quite a bit of memory, sharing them across child processes. (Modules also tend to be much easier to test than scripts.)