> I'd like to include those constants in all my templates directly from my constants module rather than importing them to my script

OK if you don't need those constants at all ...

Instead of passing the exported constants each time with ->process

>

my $tt = Template->new( { INCLUDE_PATH => $BASE_DIR . '/root/src/', INTERPOLATE => 1, } ) || die "$Template::ERROR\n"; $tt->process( $email_args{text_template},

you could make your config module export the initialized TT object

package Sam::Constants; use Template; our $tt = Template->new( # export me { INCLUDE_PATH => $BASE_DIR . '/root/src/', INTERPOLATE => 1, CONSTANTS => { ... }, VARIABLES => { ... }, } ) || die "$Template::ERROR\n";

(see also Re: Import Constants From File to Templates?)

And if things like $BASE_DIR need to be flexible, provide an import() function.

use Sam::Constants base_dir =>  "...";

edit

and if you have to manage different constructor setups, export functions which do the different construction and return the specialized $tt object.

update

package Sam::Constants; my @defaults_all = (... ); sub foo { return Template->new( @_ , @defaults_all, DEFAULTS_foo ) }; sub bar { return Template->new( @_ , @defaults_all, DEFAULTS_bar ) }; ... # other file use Sam::Constants qw/foo/; my $tt = foo(LOCAL_CFG); $tt->process();

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to Re: Import Constants From File to Templates? (outside-in) (updated) by LanX
in thread Import Constants From File to Templates? by varanasi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.