| 1 | package Template::Sandbox::Library; | ||||||||||||||||||||
| 2 | # ABSTRACT: Base library object for Template::Sandbox functions. | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 6 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 6 | ▶ | use Template::Sandbox; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 6 | my %function_library = (); | |||||||||||||||||||
| 12 | 6 | my %function_tags = (); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | BEGIN | ||||||||||||||||||||
| 15 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 6 | $Template::Sandbox::Library::VERSION = '1.04_01'; | |||||||||||||||||||
| 17 | } | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | sub import | ||||||||||||||||||||
| 20 | 13 | ▶ | { | ||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 21 | 13 | my $pkg = shift; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 13 | ▶ | export_template_functions( $pkg, 'Template::Sandbox', @_ ) if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | sub export_template_functions | ||||||||||||||||||||
| 27 | 26 | ▶ | { | ||||||||||||||||||
|
Subroutine
export_template_functions: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||
| 28 | 26 | my ( $this, $template, @imports ) = @_; | |||||||||||||||||||
| 29 | 26 | my ( $pkg, $library, $tags, %export, @functions, $existing ); | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 26 | ▶ | $pkg = ref( $this ) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | croak "\"$pkg\" does not appear to be a template function library." | ||||||||||||||||||||
| 34 | 26 | ▶ | unless $function_library{ $pkg }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 25 | $library = $function_library{ $pkg }; | |||||||||||||||||||
| 37 | 25 | $tags = $function_tags{ $pkg }; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 25 | %export = (); | |||||||||||||||||||
| 40 | 25 | foreach my $import ( @imports ) | |||||||||||||||||||
| 41 | { | ||||||||||||||||||||
| 42 | 34 | foreach my $word ( split( /\s+/, $import ) ) | |||||||||||||||||||
| 43 | { | ||||||||||||||||||||
| 44 | 34 | my ( $delete ); | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 34 | ▶ | $delete = 1 if $word =~ s/^!//; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 34 | ▶ | if( $word =~ s/^:// ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | { | ||||||||||||||||||||
| 50 | 17 | my ( @names ); | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 17 | ▶ | if( exists $tags->{ $word } ) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 53 | { | ||||||||||||||||||||
| 54 | 7 | @names = @{$tags->{ $word }}; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | elsif( $word eq 'all' ) | ||||||||||||||||||||
| 57 | { | ||||||||||||||||||||
| 58 | 9 | @names = keys( %{$library} ); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | else | ||||||||||||||||||||
| 61 | { | ||||||||||||||||||||
| 62 | 1 | croak "\"$word\" is not a template library tag in $pkg"; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 16 | ▶ | if( $delete ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | { | ||||||||||||||||||||
| 67 | 3 | delete $export{ $_ } foreach ( @names ); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | else | ||||||||||||||||||||
| 70 | { | ||||||||||||||||||||
| 71 | 13 | $export{ $_ } = 1 foreach ( @names ); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | else | ||||||||||||||||||||
| 75 | { | ||||||||||||||||||||
| 76 | 17 | ▶ | if( $delete ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | { | ||||||||||||||||||||
| 78 | 3 | delete $export{ $word }; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | else | ||||||||||||||||||||
| 81 | { | ||||||||||||||||||||
| 82 | 14 | $export{ $word } = 1; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 24 | $existing = $template->_find_local_functions(); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 24 | @functions = (); | |||||||||||||||||||
| 91 | 24 | foreach my $name ( keys( %export ) ) | |||||||||||||||||||
| 92 | { | ||||||||||||||||||||
| 93 | croak "\"$name\" is not a template library function in $pkg" | ||||||||||||||||||||
| 94 | 55 | ▶ | unless $library->{ $name }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | push @functions, $name => $library->{ $name } | ||||||||||||||||||||
| 97 | 54 | ▶ | unless exists $existing->{ $name }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 23 | ▶ | $template->register_template_function( @functions ) if @functions; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | sub set_library_functions | ||||||||||||||||||||
| 104 | 8 | ▶ | { | ||||||||||||||||||
|
Subroutine
set_library_functions: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||
| 105 | 8 | my ( $this, %functions ) = @_; | |||||||||||||||||||
| 106 | 8 | my ( $pkg ); | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 8 | ▶ | $pkg = ref( $this ) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 8 | $function_library{ $pkg } = \%functions; | |||||||||||||||||||
| 111 | 8 | ▶ | $function_tags{ $pkg } ||= {}; | ||||||||||||||||||
|
Condition50%1/2
$function_tags{$pkg} ||= {}
MC/DC0%0/1
$function_tags{$pkg} ||= {}
$function_tags{$pkg}
Truth table50%1/2
A
A= $function_tags{$pkg}
| |||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | sub set_library_tags | ||||||||||||||||||||
| 115 | 4 | ▶ | { | ||||||||||||||||||
|
Subroutine
set_library_tags: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||
| 116 | 4 | my ( $this, %tags ) = @_; | |||||||||||||||||||
| 117 | 4 | my ( $pkg ); | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 4 | ▶ | $pkg = ref( $this ) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 4 | $function_tags{ $pkg } = \%tags; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 6 | 1; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||