1 #================================================================= -*-Perl-*-
3 # Template::Namespace::Constants
6 # Plugin compiler module for performing constant folding at compile time
7 # on variables in a particular namespace.
10 # Andy Wardley <abw@wardley.org>
13 # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
18 #============================================================================
20 package Template::Namespace::Constants;
24 use base 'Template::Base';
26 use Template::Directive;
27 use Template::Exception;
30 our $DEBUG = 0 unless defined $DEBUG;
34 my ($self, $config) = @_;
35 $self->{ STASH } = Template::Config->stash($config)
36 || return $self->error(Template::Config->error());
42 #------------------------------------------------------------------------
43 # ident(\@ident) foo.bar(baz)
44 #------------------------------------------------------------------------
47 my ($self, $ident) = @_;
50 # discard first node indicating constants namespace
51 splice(@$ident, 0, 2);
53 my $nelems = @$ident / 2;
57 print STDERR "constant ident [ @$ident ] " if $DEBUG;
59 foreach $e (0..$nelems-1) {
60 # node name must be a constant
61 unless ($ident->[$e * 2] =~ s/^'(.+)'$/$1/s) {
62 $self->DEBUG(" * deferred (non-constant item: ", $ident->[$e * 2], ")\n")
64 return Template::Directive->ident(\@save);
67 # if args is non-zero then it must be eval'ed
68 if ($ident->[$e * 2 + 1]) {
69 my $args = $ident->[$e * 2 + 1];
70 my $comp = eval "$args";
72 $self->DEBUG(" * deferred (non-constant args: $args)\n") if $DEBUG;
73 return Template::Directive->ident(\@save);
75 $self->DEBUG("($args) ") if $comp && $DEBUG;
76 $ident->[$e * 2 + 1] = $comp;
81 $result = $self->{ STASH }->get($ident);
83 if (! length $result || ref $result) {
84 my $reason = length $result ? 'reference' : 'no result';
85 $self->DEBUG(" * deferred ($reason)\n") if $DEBUG;
86 return Template::Directive->ident(\@save);
91 $self->DEBUG(" * resolved => '$result'\n") if $DEBUG;
102 Template::Namespace::Constants - Compile time constant folding
106 # easy way to define constants
109 my $tt = Template->new({
116 # nitty-gritty, hands-dirty way
117 use Template::Namespace::Constants;
119 my $tt = Template->new({
121 constants => Template::Namespace::Constants->new({
130 The C<Template::Namespace::Constants> module implements a namespace handler
131 which is plugged into the C<Template::Directive> compiler module. This then
132 performs compile time constant folding of variables in a particular namespace.
136 =head2 new(\%constants)
138 The new() constructor method creates and returns a reference to a new
139 Template::Namespace::Constants object. This creates an internal stash
140 to store the constant variable definitions passed as arguments.
142 my $handler = Template::Namespace::Constants->new({
147 =head2 ident(\@ident)
149 Method called to resolve a variable identifier into a compiled form. In this
150 case, the method fetches the corresponding constant value from its internal
151 stash and returns it.
155 Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
159 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
161 This module is free software; you can redistribute it and/or
162 modify it under the same terms as Perl itself.
166 L<Template::Directive>
172 # perl-indent-level: 4
173 # indent-tabs-mode: nil
176 # vim: expandtab shiftwidth=4: