| 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
| |||||||||||||
| 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
| |||||||||||||
| 57 | 99 | ▶ | if ($item =~ /\A:/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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}'!")
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}
| |||||||||||||
| 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
| |||||||||||||
| 91 | |||||||||||||
| 92 | 75 | ▶ | if (UNIVERSAL::can($plugin, 'install')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 106 | |||||||||||||
| 107 | 76 | ▶ | if (my $plugin_class = $class->load_plugin( $plugin )) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 120 | |||||||||||||
| 121 | 75 | eval "require $plugin_class"; | |||||||||||
| 122 | 75 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | 1 | ▶ | warnings::warn("error loading $plugin plugin: $@") if warnings::enabled; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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__ | ||||||||||||