| 1 | #================================================================= -*-Perl-*- | ||
| 2 | # | ||
| 3 | # Template::Namespace::Constants | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Plugin compiler module for performing constant folding at compile time | ||
| 7 | # on variables in a particular namespace. | ||
| 8 | # | ||
| 9 | # AUTHOR | ||
| 10 | # Andy Wardley <abw@wardley.org> | ||
| 11 | # | ||
| 12 | # COPYRIGHT | ||
| 13 | # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. | ||
| 14 | # | ||
| 15 | # This module is free software; you can redistribute it and/or | ||
| 16 | # modify it under the same terms as Perl itself. | ||
| 17 | # | ||
| 18 | #============================================================================ | ||
| 19 | |||
| 20 | package Template::Namespace::Constants; | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use base 'Template::Base'; | ||
| 25 | use Template::Config; | ||
| 26 | use Template::Directive; | ||
| 27 | use Template::Exception; | ||
| 28 | |||
| 29 | our $VERSION = '3.105'; | ||
| 30 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 31 | |||
| 32 | |||
| 33 | sub _init { | ||
| 34 | my ($self, $config) = @_; | ||
| 35 | $self->{ STASH } = Template::Config->stash($config) | ||
| 36 | || return $self->error(Template::Config->error()); | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | #------------------------------------------------------------------------ | ||
| 43 | # ident(\@ident) foo.bar(baz) | ||
| 44 | #------------------------------------------------------------------------ | ||
| 45 | |||
| 46 | sub ident { | ||
| 47 | my ($self, $ident) = @_; | ||
| 48 | my @save = @$ident; | ||
| 49 | |||
| 50 | # discard first node indicating constants namespace | ||
| 51 | splice(@$ident, 0, 2); | ||
| 52 | |||
| 53 | my $nelems = @$ident / 2; | ||
| 54 | my ($e, $result); | ||
| 55 | local $" = ', '; | ||
| 56 | |||
| 57 | print STDERR "constant ident [ @$ident ] " if $DEBUG; | ||
| 58 | |||
| 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") | ||
| 63 | if $DEBUG; | ||
| 64 | return Template::Directive->ident(\@save); | ||
| 65 | } | ||
| 66 | |||
| 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"; | ||
| 71 | if ($@) { | ||
| 72 | $self->DEBUG(" * deferred (non-constant args: $args)\n") if $DEBUG; | ||
| 73 | return Template::Directive->ident(\@save); | ||
| 74 | } | ||
| 75 | $self->DEBUG("($args) ") if $comp && $DEBUG; | ||
| 76 | $ident->[$e * 2 + 1] = $comp; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | $result = $self->{ STASH }->get($ident); | ||
| 82 | |||
| 83 | if (! length $result) { | ||
| 84 | $self->DEBUG(" * deferred (no result)\n") if $DEBUG; | ||
| 85 | return Template::Directive->ident(\@save); | ||
| 86 | } | ||
| 87 | |||
| 88 | if (ref $result) { | ||
| 89 | require Data::Dumper; | ||
| 90 | local $Data::Dumper::Terse = 1; | ||
| 91 | local $Data::Dumper::Indent = 0; | ||
| 92 | local $Data::Dumper::Sortkeys = 1; | ||
| 93 | my $code = Data::Dumper::Dumper($result); | ||
| 94 | |||
| 95 | $self->DEBUG(" * resolved ref => $code\n") if $DEBUG; | ||
| 96 | |||
| 97 | return $code; | ||
| 98 | } | ||
| 99 | |||
| 100 | $result =~ s/'/\\'/g; | ||
| 101 | |||
| 102 | $self->DEBUG(" * resolved => '$result'\n") if $DEBUG; | ||
| 103 | |||
| 104 | return "'$result'"; | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||
| 108 | |||
| 109 | __END__ |