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:
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?
Yes. It's late. Get some sleep before you start hacking.Any thoughts?
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,
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.)