http://qs321.pair.com?node_id=524513

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

Hello everybody.

I have a module here I've been working on that generates Perl code from templates for a purpose particular to its domain.

I'm using the Template Toolkit for my templates and I'm very happy with that. I've got the templates themselves in a subdir called /root.

Now all is well when working locally but I want users to be able to unpack and install my module and the module have access to the templates to do its work.

How might this be possible? Well, I know from the TT docs that Template::Config provides the instdir method which lets you find the location of your TT2 installation and that there's a templates directory there. So maybe some tinkering with the Makefile could get them installed...

...but hang on: what if the module gets installed by someone without superuser privileges or in a non-standard environment for testing purposes?

I note that Maypole/Catalyst have TT2 templates as part of the distribution and have gone down the path of telling you to copy them into your derived application. Is there a tidier way?

Obviously, it's occurred to me to inline them in Perl HEREDOCs but that's not very tidy and I wondered if anyone could do better?


MB

Replies are listed 'Best First'.
Re: Including TT2 templates in module distributions
by ikegami (Patriarch) on Jan 20, 2006 at 16:31 UTC

    You could put them in a dir relative to your module, then use %INC to locate them.

    use My::Module; use File::Spec; my $path = $INC{'My/Module.pm'}; $path = File::Spec->rel2abs($path); my ($vol, $dir, $file) = File::Spec->splitpath($path); $path = File::Spec->catpath($vol, $dir, ''); $path = File::Spec->catdir($path, 'templates'); print("$path\n");

    Update: splitpath didn't work with relative directories. Fixed.

      Yeah, that'll do it.

      Marvellous, thanks very much.


      MB
Re: Including TT2 templates in module distributions
by ides (Deacon) on Jan 20, 2006 at 17:09 UTC
    You could also look at Inline::TT or use something like Module::Build or Module::Install to handle installing the templates into a known location for the user.

    Frank Wiles <frank@revsys.com>
    www.revsys.com

Re: Including TT2 templates in module distributions
by brian_d_foy (Abbot) on Jan 20, 2006 at 21:41 UTC

    If you are using ExtUtils::MakeMaker, you can prompt the user for an installation location (or just set it yourself). To actually install that stuff using the usual Makemaker sequence, you add some makefile rules.

    Makemaker uses the My:: namespace to allow you to add extra stuff (or override things) that Makemaker will output. You just need to add the stuff to install your templates.

    The targets in the makefile are double-colon targets, so you can add to their definition. From the postamble(), return the string you want Makemaker to add to the makefile.

    { package MY; sub postamble { return <<"HERE"; all :: template_all install :: template_install template_all :: ...dependencies... # ...commands to run to make templates template_install :: ...dependencies... # ...commands to install templates HERE } }

    Makemaker has a lot more features like this, too. The best way to find out about them is to look at other people's Makefile.PL. The complicated modules should have interesting examples that you can re-use. Once you get the hang of it, you can do some pretty powerful things.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review