in reply to Import Constants From File to Templates?

I'm no Template::Toolkit expert and and it's not clear to me how you wanna use the constants, since you gave no example°.

I just skimmed thru the docs and found two possible approaches:

Constants in Perl are just implemented as subs with empty prototype

Hence you could write a sub current_constants() which is introspecting your current package and return a list of pairs which maps each constant to it's name. Another way could be to check the exporter settings of the defining package.

my $tt = Template->new({ CONSTANTS => { current_constants() }, });

Once this works you should also be able to write a TT plugin to do this automatically.

Question That's what you want?

update

°) this was written before the OP added his "EDIT" section.

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

Replies are listed 'Best First'.
Re^2: Import Constants From File to Templates?
by Fletch (Bishop) on Dec 04, 2021 at 14:31 UTC

    Variation on the second approach you could load your values from an YAML file (or similarly JSON if that's your bag) rather than from code:

    use Template; use YAML::XS qw( Load LoadFile ); use Cpanel::JSON::XS qw( decode_json ); ## This could be an external file, and/or make this a plugin argument +that points to it. my $consts = Load( <<'EOT' ); --- PRE: 1 REGULAR: 2 POST: 3 EOT ## Alternately my $alternate = decode_json( <<'EOT' ); {"PRE": 1, "REGULAR": 2, "POST": 3, "Foo": "Bar" } EOT my $template = Template->new({ CONSTANTS => { y => $consts, j => $alternate }, CONSTANTS_NAMESPACE => 'C', }); __END__ [% IF type == C.j.REGULAR %] Blah blah I R REGULAR stuff. But JSON says foo is '[% c.j.Foo %]' [% END %]

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^2: Import Constants From File to Templates? (introspect current constants) (updated)
by LanX (Saint) on Dec 04, 2021 at 15:04 UTC
    > Hence you could write a sub current_constants() which is introspecting your current package and return a list of pairs which maps each constant to it's name.

    Jeez, that was harder than I thought. Perl is also storing constants as scalar-refs if no glob is needed for other slots.

    (NB: I'm now returning a hash-ref)

    use strict; use warnings; #use Data::Dump; sub current_constants { #warn my ($pkg) = caller; my @result; no strict 'refs'; while ( my ($key,$val) = each %{"${pkg}::"} ) { #warn "*** ${pkg}::$key =>$val <", ref($val),">\n"; if (ref $val eq "SCALAR") { push @result, $key, $$val; next; } if ( defined (my $c_ref = *$val{CODE}) ) { my $proto = prototype $c_ref; if ( defined $proto && $proto eq "") { push @result, $key, &$c_ref; } } } return { @result }; } package Some::Package; use Test::More; use constant TEST1 => "TEST1"; use constant TEST2 => "TEST2"; sub const() {"const"}; # those should be ignored our $TEST1 = "bla"; our $scl = "Scalar"; our %hsh = (Hash=>"Hash"); our @arr = ("Array"); sub func {"bla"}; is_deeply( main::current_constants(), { const => "const", TEST1 => "TEST1", TEST2 => "TEST2" } ); done_testing;

    update

    just read again

    > I'd like to include those constants in all my templates directly from my constants module rather than importing them to my script and then remembering to set the ones I need each time I process a template.

    Well I don't know how to globally include constants into all TT templates, but please note that my solution to find doesn't require to import them first.

    Just change the sub to get the $pkg origin from an argument instead from caller..

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