Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

gav (2710)

  (email not shown publicly)
AOL IM: flufflegavin (Add Buddy, Send Message)

Hacker in NYC.

Journal of gav (2710)

Wednesday September 11, 2002
10:41 PM

wacky idea

[ #7691 ]

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?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • 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

  • 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.)