| 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 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 6 | ▶ | use base 'Template::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 6 | ▶ | use Template::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 6 | ▶ | use Template::Directive; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 6 | ▶ | use Template::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 6 | our $VERSION = '3.106'; | |||||||||||||||||||||||||||
| 30 | 6 | ▶ | our $DEBUG = 0 unless defined $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 11 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 34 | 11 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 35 | 11 | ▶ | $self->{ STASH } = Template::Config->stash($config) | ||||||||||||||||||||||||||
|
Condition50%1/2
'Template::Config'->stash($config) || (return $self->error('Template::Config'->error))
MC/DC0%0/1
'Template::Config'->stash($config) || (return $self->error('Template::Config'->error))
'Template::Config'->stash($config)
Truth table50%1/2
A
A= 'Template::Config'->stash($config)
| |||||||||||||||||||||||||||||
| 36 | || return $self->error(Template::Config->error()); | ||||||||||||||||||||||||||||
| 37 | 11 | return $self; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 43 | # ident(\@ident) foo.bar(baz) | ||||||||||||||||||||||||||||
| 44 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 44 | ▶ | sub ident { | ||||||||||||||||||||||||||
|
Subroutine
ident: called CC=15 SCAR=29.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 44 | my ($self, $ident) = @_; | |||||||||||||||||||||||||||
| 48 | 44 | my @save = @$ident; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # discard first node indicating constants namespace | ||||||||||||||||||||||||||||
| 51 | 44 | splice(@$ident, 0, 2); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 44 | my $nelems = @$ident / 2; | |||||||||||||||||||||||||||
| 54 | 44 | my ($e, $result); | |||||||||||||||||||||||||||
| 55 | 44 | local $" = ', '; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 44 | ▶ | print STDERR "constant ident [ @$ident ] " if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 44 | foreach $e (0..$nelems-1) { | |||||||||||||||||||||||||||
| 60 | # node name must be a constant | ||||||||||||||||||||||||||||
| 61 | 74 | ▶ | unless ($ident->[$e * 2] =~ s/^'(.+)'$/$1/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $self->DEBUG(" * deferred (non-constant item: ", $ident->[$e * 2], ")\n") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | if $DEBUG; | ||||||||||||||||||||||||||||
| 64 | 0 | return Template::Directive->ident(\@save); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # if args is non-zero then it must be eval'ed | ||||||||||||||||||||||||||||
| 68 | 74 | ▶ | if ($ident->[$e * 2 + 1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 4 | my $args = $ident->[$e * 2 + 1]; | |||||||||||||||||||||||||||
| 70 | 4 | my $comp = eval "$args"; | |||||||||||||||||||||||||||
| 71 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $self->DEBUG(" * deferred (non-constant args: $args)\n") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | return Template::Directive->ident(\@save); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 4 | ▶ | $self->DEBUG("($args) ") if $comp && $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$comp and $Template::Namespace::Constants::DEBUG
MC/DC0%0/2
$comp and $Template::Namespace::Constants::DEBUG
$comp
$Template::Namespace::Constants::DEBUG
Truth table33%1/3
A and B
A= $compB= $Template::Namespace::Constants::DEBUG
| |||||||||||||||||||||||||||||
| 76 | 4 | $ident->[$e * 2 + 1] = $comp; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 44 | $result = $self->{ STASH }->get($ident); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 44 | ▶ | if (! length $result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1 | ▶ | $self->DEBUG(" * deferred (no result)\n") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 1 | return Template::Directive->ident(\@save); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 43 | ▶ | if (ref $result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 4 | require Data::Dumper; | |||||||||||||||||||||||||||
| 90 | 4 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 91 | 4 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||
| 92 | 4 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 93 | 4 | my $code = Data::Dumper::Dumper($result); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 4 | ▶ | $self->DEBUG(" * resolved ref => $code\n") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 4 | return $code; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 39 | $result =~ s/'/\\'/g; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 39 | ▶ | $self->DEBUG(" * resolved => '$result'\n") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 39 | return "'$result'"; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 6 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||