Utils.pm

filter: Statementstmt 91.5% 54 / 59 Branchbran 70.0% 14 / 20 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 7 totaltotal 81.1% 82 / 101 CC 13 SCAR 10.1
CC
13
Cov
86%
install
4.0
load_plugin
4.0
install_plugin
3.1
CRAP 2.7
« Utils/Like.pm ↑ Summary Utils/Decode.pm »
1 package Petal::Utils;
2  
3 =head1 NAME
4  
5 Petal::Utils - Useful template modifiers for Petal.
6  
7 =head1 SYNOPSIS
8  
9 # install the default set of Petal modifiers:
10 use Petal::Utils;
11  
12 # you can also install modifiers manually:
13 Petal::Utils->install( 'some_modifier', ':some_set' );
14  
15 # see below for modifiers available & template syntax
16  
17 =cut
18  
19 15 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 15 ▶use Petal::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 15 our $VERSION = '0.06';
26 15 our $DEBUG = 0;
27  
28 #------------------------------------------------------------------------------
29 # Cusomized import() so the user can select different plugins & sets
30  
31 # use an Exporter-like syntax here:
32 15 our %PLUGIN_SET =
33 (
34 ':none' => [],
35 ':all' => [qw( :default :hash :debug )],
36 ':default' => [qw( :text :date :logic :list :uri )],
37 ':text' => [qw( UpperCase LowerCase UC_First Substr Printf )],
38 ':logic' => [qw( And If Or Equal Like Decode )],
39 ':date' => [qw( Date US_Date )],
40 ':list' => [qw( Sort Limit Limitr)],
41 ':hash' => [qw( Each Keys )],
42 ':uri' => [qw( UriEscape Create_Href )],
43 ':debug' => [qw( Dump )],
44 );
45  
46 18 ▶sub import {
Subroutine import: called CC=2 SCAR=7.1
47 18 my $class = shift;
48 18 ▶ push @_, ':default' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 18 18
49 18 return $class->install( @_ );
50 }
51  
52 40 ▶sub install {
Subroutine install: called CC=4 SCAR=13.9
Pod undocumented
53 40 my $class = shift;
54  
55 40 foreach my $item (@_) {
56 99 ▶ next unless $item;
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 99 99
57 99 ▶ if ($item =~ /\A:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /\A:/) { } 23 76 99
58 23 $class->install_plugin_set( $item );
59 } else {
60 76 $class->install_plugin( $item );
61 }
62 }
63  
64 38 return $class;
65 }
66  
67 23 ▶sub install_plugin_set {
Subroutine install_plugin_set: called CC=2 SCAR=6.9
Pod undocumented
68 23 my $class = shift;
69 23 my $set = shift;
70  
71 23 ▶ my $plugins = $PLUGIN_SET{$set}
Condition100%2/2
$Petal::Utils::PLUGIN_SET{$set} || die("Can't install non-existent plugin set '${set}'!")
l!l
221
MC/DC100%1/1
$Petal::Utils::PLUGIN_SET{$set} || die("Can't install non-existent plugin set '${set}'!")
$Petal::Utils::PLUGIN_SET{$set}
Truth table100%2/2
A
A= $Petal::Utils::PLUGIN_SET{$set}
Aresult
11
00
72 || die "Can't install non-existent plugin set '$set'!";
73  
74 # recursive so we can have sets of sets:
75 22 $class->install( @$plugins );
76 }
77  
78 0 ▶sub install_plugins {
Subroutine install_plugins: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my $class = shift;
80 0 map { $class->install_plugin( $_ ) } @_;
81 0 return $class;
82 }
83  
84 76 ▶sub install_plugin {
Subroutine install_plugin: called CC=3 SCAR=11.4
Pod undocumented
85 76 my $class = shift;
86 76 my $name = shift;
87  
88 76 my $plugin = $class->find_plugin( $name );
89  
90 75 ▶ warn "installing Petal plugin: '$name'\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Petal::Utils::DEBUG 0 75 75
91  
92 75 ▶ if (UNIVERSAL::can($plugin, 'install')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($plugin, 'install')) { } 75 0 75
93 75 $plugin->install;
94 } else {
95 0 $Petal::Hash::MODIFIERS->{"$plugin:"} = $plugin;
96 }
97  
98 75 return $class;
99 }
100  
101 76 ▶sub find_plugin {
Subroutine find_plugin: called CC=3 SCAR=11.0
Pod undocumented
102 76 my $class = shift;
103 76 my $plugin = shift;
104  
105 76 ▶ return \&$plugin if $class->can( $plugin );
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($plugin) 0 76 76
106  
107 76 ▶ if (my $plugin_class = $class->load_plugin( $plugin )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugin_class = $class->load_plugin($plugin)) 75 1 76
108 75 return $plugin_class;
109 }
110  
111 1 die "Can't find Petal plugin: '$plugin'!";
112 }
113  
114 76 ▶sub load_plugin {
Subroutine load_plugin: called CC=4 SCAR=13.9
Pod undocumented
115 76 my $class = shift;
116 76 my $plugin = shift;
117  
118 76 my $plugin_class = $class->get_plugin_class_for( $plugin );
119 76 ▶ return $plugin_class if $plugin_class->can( 'process' );
Branch: 1 branch
BranchTrueFalseTotal
if $plugin_class->can("process") 1 75 76
120  
121 75 eval "require $plugin_class";
122 75 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 74 75
123 1 ▶ warnings::warn("error loading $plugin plugin: $@") if warnings::enabled;
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 1 1
124 1 return;
125 }
126  
127 74 return $plugin_class;
128 }
129  
130 76 ▶sub get_plugin_class_for {
Subroutine get_plugin_class_for: called CC=1 SCAR=0.0
Pod undocumented
131 76 my $class = shift;
132 76 my $plugin = shift;
133 76 my $plugin_class = "$class\::$plugin";
134 }
135  
136  
137 #------------------------------------------------------------------------------
138 # Plugins
139  
140 ## See Petal::Utils::<plugin> for plugin classes
141 ## (plugins are now loaded as needed)
142  
143 ## Alternatively, use subs to insert new modifiers into the Petal Modifiers
144 ## hash. Note that we do not get the $class value in this format.
145  
146 # This style is deprecated:
147 # sub foo {
148 # my $hash = shift;
149 # my $args = shift;
150 # my $result = $hash->fetch( $args );
151 # return 'foo '.$result;
152 # }
153  
154 15 1;
155  
156 __END__
« Utils/Like.pm ↑ Summary Utils/Decode.pm »