| 1 | package List::Filter::Storage::CODE; | ||||||||||||||||||||
| 2 | #use base qw( List::Filter::StorageBase ); | ||||||||||||||||||||
| 3 | 1 | ▶ | use base qw( List::Filter::Storage::MEM ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | List::Filter::Storage::CODE - reads in standard libraries of filters from code | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | # This is a plugin, not intended for direct use. | ||||||||||||||||||||
| 12 | # See: List:Filter:Storage | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | use List::Filter::Storage::CODE; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # load all of transforms from standard library module location | ||||||||||||||||||||
| 17 | my $storage = List::Filter::Storage::CODE->new({ | ||||||||||||||||||||
| 18 | type => 'transform', | ||||||||||||||||||||
| 19 | }); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # load just the specified filter libraries | ||||||||||||||||||||
| 22 | # (Note: allows non-standard locations, and/or conserves memory) | ||||||||||||||||||||
| 23 | my $storage = List::Filter::Storage::CODE->new({ | ||||||||||||||||||||
| 24 | connect_to => [ Some::Odd::Library::Module And::Another ], | ||||||||||||||||||||
| 25 | type => 'filter', | ||||||||||||||||||||
| 26 | }); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # Retrieving a filter | ||||||||||||||||||||
| 29 | my $filter = $storage->lookup( ':omit' ); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | The L<List::Filter> project ships with some standard filters defined | ||||||||||||||||||||
| 34 | in perl code. The "CODE" storage location format allows these filters | ||||||||||||||||||||
| 35 | to be looked up by name using the standard storage interface. | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | This format may be used in two ways: | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | (1) by default it will slurp in all definitions found in all of | ||||||||||||||||||||
| 40 | the modules found in the standard library location for the data | ||||||||||||||||||||
| 41 | type (e.g. for "filter" it will look in | ||||||||||||||||||||
| 42 | "List::Filter::Library::*" , for "transform" it will look in | ||||||||||||||||||||
| 43 | "List::Filter::Transform::Library::*"). | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | (2) It can be provided with a list of libary names, (which should | ||||||||||||||||||||
| 46 | correspond to file names: <*>.pm), and it will only load the | ||||||||||||||||||||
| 47 | filters from those particular files. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =head2 METHODS | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =over | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 1 | ▶ | use 5.8.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 57 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 58 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 59 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 60 | 1 | ▶ | use Hash::Util qw( lock_keys unlock_keys ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 61 | 1 | ▶ | use Module::List::Pluggable qw( list_modules_under import_modules ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||
| 64 | 1 | my $DEBUG = 0; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =item new | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Instantiates a new List::Filter::Storage::CODE object. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | With no arguments, the newly created profile will be loaded | ||||||||||||||||||||
| 71 | with all filters from the appropriate installed code libraries. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | If connect_to is defined as a list of library module names, | ||||||||||||||||||||
| 74 | it will load only those library modules. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # Note: "new" (inherited from Class::Base) | ||||||||||||||||||||
| 79 | # calls the following "init" routine automatically. | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =item init | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | Initialize object attributes and then lock them down to prevent | ||||||||||||||||||||
| 84 | accidental creation of new ones. | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | Note: there is no leading underscore on name "init", though it's | ||||||||||||||||||||
| 87 | arguably an "internal" routine (i.e. not likely to be of use to | ||||||||||||||||||||
| 88 | client code). | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 8 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 93 | 8 | my $self = shift; | |||||||||||||||||||
| 94 | 8 | my $args = shift; | |||||||||||||||||||
| 95 | 8 | unlock_keys( %{ $self } ); | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 8 | $self->SUPER::init( $args ); # uncomment if this is a child class | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 8 | my $lib_href = {}; | |||||||||||||||||||
| 100 | 8 | my $libraries_list = $args->{ connect_to }; | |||||||||||||||||||
| 101 | 8 | ▶ | if ( $libraries_list ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 2 | $lib_href = $self->load_given_libraries( $libraries_list ); | |||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||
| 104 | 6 | $lib_href = $self->load_all_libraries; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 8 | $self->set_filter_data( $lib_href ); | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 8 | lock_keys( %{ $self } ); | |||||||||||||||||||
| 110 | 8 | return $self; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =item lookup | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | See "lookup" in L<List::Filter::Storage::MEM/lookup> | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =item save | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 0 | ▶ | sub save { | ||||||||||||||||||
|
Subroutine
save: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 124 | 0 | my $self = shift; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | $self->debug( "The 'save' method is not implemented for the 'CODE' storage format" ); | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | return 0; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =back | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head2 internally used methods | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =over | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =item define_library_location | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | From the type of the stored filters (e.g. 'filter', 'transform'), | ||||||||||||||||||||
| 141 | determine the appropriate filter library location in the perl | ||||||||||||||||||||
| 142 | module namespace. | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | This implements the convention: | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | List::Filter::<upper-case type>::Library, except that for | ||||||||||||||||||||
| 147 | type 'filter' the class is just "List::Filter::Library::". | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Gets "type" from the object data, unless supplied as an argument. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 6 | ▶ | sub define_library_location { | ||||||||||||||||||
|
Subroutine
define_library_location: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||
| 154 | 6 | my $self = shift; | |||||||||||||||||||
| 155 | 6 | ▶ | my $type = shift || $self->type; | ||||||||||||||||||
|
Condition33%1/3
shift() || $self->type
MC/DC0%0/2
shift() || $self->type
shift()
$self->type
Truth table33%1/3
A || B
A= shift()B= $self->type
| |||||||||||||||||||||
| 156 | 6 | my $library_location; | |||||||||||||||||||
| 157 | 6 | ▶ | if (not ($type) ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 158 | 0 | croak "define_library_location in CODE.pm at line " . | |||||||||||||||||||
| 159 | __LINE__ . | ||||||||||||||||||||
| 160 | ": needs a defined 'type' (e.g. 'filter', 'transform')."; | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | } elsif ($type eq 'filter') { | ||||||||||||||||||||
| 163 | 4 | $library_location = 'List::Filter::Library'; | |||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||
| 165 | 2 | $library_location = 'List::Filter::' . ucfirst( $type ) . '::Library'; | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | 6 | return $library_location; | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =item load_all_libraries | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Loads all available libraries of filters. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | The type ('filter', 'transform', etc.) will come from the | ||||||||||||||||||||
| 175 | object data, unless passed in as a second argument. | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =cut | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 6 | ▶ | sub load_all_libraries { | ||||||||||||||||||
|
Subroutine
load_all_libraries: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 180 | 6 | my $self = shift; | |||||||||||||||||||
| 181 | 6 | ▶ | my $type = shift || $self->type; | ||||||||||||||||||
|
Condition33%1/3
shift() || $self->type
MC/DC0%0/2
shift() || $self->type
shift()
$self->type
Truth table33%1/3
A || B
A= shift()B= $self->type
| |||||||||||||||||||||
| 182 | 6 | my $library_location = $self->define_library_location( $type ); | |||||||||||||||||||
| 183 | 6 | my $library_names = list_modules_under( $library_location ); | |||||||||||||||||||
| 184 | 6 | my $library_href = $self->load_given_libraries( $library_names ); | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 6 | return $library_href; | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =item load_given_libraries | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | Loads all requested libraries of filters, from the tree of | ||||||||||||||||||||
| 194 | libraries for the type ('filter', 'transform'). This type | ||||||||||||||||||||
| 195 | must be passed in in the attributes hash reference. | ||||||||||||||||||||
| 196 | The given list of names should be an aref of module names. | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | The type ('filter', 'transform', etc.) will come from the | ||||||||||||||||||||
| 199 | object data, unless passed in as a second argument. | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | # Note, using this feature you can get it to look in nonstandard | ||||||||||||||||||||
| 204 | # locations. | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 8 | ▶ | sub load_given_libraries { | ||||||||||||||||||
|
Subroutine
load_given_libraries: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||
| 207 | 8 | my $self = shift; | |||||||||||||||||||
| 208 | 8 | my $library_names = shift; | |||||||||||||||||||
| 209 | 8 | ▶ | my $type = shift || $self->type; | ||||||||||||||||||
|
Condition33%1/3
shift() || $self->type
MC/DC0%0/2
shift() || $self->type
shift()
$self->type
Truth table33%1/3
A || B
A= shift()B= $self->type
| |||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 8 | my $filter_lib = {}; | |||||||||||||||||||
| 212 | 8 | foreach my $library (@{ $library_names }) { | |||||||||||||||||||
| 213 | 12 | eval "require $library"; | |||||||||||||||||||
| 214 | 12 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 215 | 0 | carp "Problem with require $library:$@"; | |||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | 12 | my $lib = $library->new(); | |||||||||||||||||||
| 218 | 12 | my $new_lib = $lib->define_filters_href(); | |||||||||||||||||||
| 219 | 12 | merge_hash( $filter_lib, $new_lib ); | |||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 8 | return $filter_lib; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =back | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =head2 proceedural routines | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | =over | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =item merge_hash | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | This routine does hash addition, merging the key-value pairs of | ||||||||||||||||||||
| 235 | one hash into another. | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | It takes two hash references, and adds the values of the second | ||||||||||||||||||||
| 238 | into the first. | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | Inputs: (1) the summation href, (2) the href to be added into the first. | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | Return: a copy of the summation href, for convenience: don't assume | ||||||||||||||||||||
| 243 | that the first argument isn't modified. | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | 12 | ▶ | sub merge_hash { | ||||||||||||||||||
|
Subroutine
merge_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 248 | 12 | my $big_hash = shift; | |||||||||||||||||||
| 249 | 12 | my $add_hash = shift; | |||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | 12 | my @keys = (keys %{ $add_hash }); | |||||||||||||||||||
| 252 | 12 | @{ $big_hash }{ @keys } = @{ $add_hash }{ @keys }; | |||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | 12 | return $big_hash; | |||||||||||||||||||
| 255 | } | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | =back | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | =head2 basic setters and getters | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | See L<List::Filter::StorageBase> for the basic accessors. | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 1 | 1; | |||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | =head1 SEE ALSO | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | L<List::Filter::Project> | ||||||||||||||||||||
| 270 | L<List:Filter:Storage> | ||||||||||||||||||||
| 271 | L<List::Filter> | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | =head1 AUTHOR | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt>, | ||||||||||||||||||||
| 276 | 24 May 2007 | ||||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | Copyright (C) 2007 by Joseph Brenner | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 283 | it under the same terms as Perl itself, either Perl version 5.8.2 or, | ||||||||||||||||||||
| 284 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | =head1 BUGS | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | None reported... yet. | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | =cut | ||||||||||||||||||||