Declare/Meta.pm

filter: Statementstmt 93.8% 153 / 163 Branchbran 65.9% 29 / 44 Conditioncond 85.7% 12 / 14 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 90.3% 28 / 31 Podpod 100% 8 / 8 totaltotal 88.0% 236 / 268 CC 33 SCAR 13.1
CC
33
Cov
88%
get_ref_from_package
8.1
is_tag
6.0
is_argument
6.0
CRAP 3.7
« Declare.pm ↑ Summary Declare/Export.pm »
1 package Exporter::Declare::Meta;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Scalar::Util qw/blessed reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use aliased 'Exporter::Declare::Export::Sub';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use aliased 'Exporter::Declare::Export::Variable';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use aliased 'Exporter::Declare::Export::Alias';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Meta::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 accessor 'export_meta';
13  
14 hash_metric exports => (
15 111 ▶ add => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
16 111 my $self = shift;
17 111 my ( $data, $metric, $action, $item, $ref ) = @_;
18 111 ▶ croak "Exports must be instances of 'Exporter::Declare::Export'"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($ref) and $ref->isa("Exporter::Declare::Export") 1 110 111
Condition66%2/3
builtin::blessed($ref) and $ref->isa("Exporter::Declare::Export")
!ll&&!rl&&r
10110
MC/DC50%1/2
builtin::blessed($ref) and $ref->isa("Exporter::Declare::Export")
builtin::blessed($ref) $ref->isa("Exporter::Declare::Export")
Truth table66%2/3
A and B
A= builtin::blessed($ref)B= $ref->isa("Exporter::Declare::Export")
ABresult
0X0
100
111
19 unless blessed($ref) && $ref->isa('Exporter::Declare::Export');
20  
21 110 my ( $type, $name ) = ( $item =~ m/^([\&\%\@\$])?(.*)$/ );
22 110 ▶ $type ||= '&';
Condition100%2/2
$type ||= "&"
l!l
2585
MC/DC100%1/1
$type ||= "&"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
23 110 my $fullname = "$type$name";
24  
25 110 $self->default_hash_add( $data, $metric, $action, $fullname, $ref );
26  
27 109 push @{$self->export_tags->{all}} => $fullname;
28 },
29 108 ▶ get => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
30 108 my $self = shift;
31 108 my ( $data, $metric, $action, $item ) = @_;
32  
33 108 ▶ croak "exports_get() does not accept a tag as an argument"
Branch: 1 branch
BranchTrueFalseTotal
if $item =~ /^[:-]/ 2 106 108
34 if $item =~ m/^[:-]/;
35  
36 106 my ( $type, $name ) = ( $item =~ m/^([\&\%\@\$])?(.*)$/ );
37 106 ▶ $type ||= '&';
Condition100%2/2
$type ||= "&"
l!l
979
MC/DC100%1/1
$type ||= "&"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
38 106 my $fullname = "$type$name";
39  
40 106 ▶ return $self->default_hash_get( $data, $metric, $action, $fullname )
Condition100%2/2
$self->default_hash_get($data, $metric, $action, $fullname) || croak($self->package . " does not export '${fullname}'")
l!l
1051
MC/DC100%1/1
$self->default_hash_get($data, $metric, $action, $fullname) || croak($self->package . " does not export '${fullname}'")
$self->default_hash_get($data, $metric, $action, $fullname)
Truth table100%2/2
A
A= $self->default_hash_get($data, $metric, $action, $fullname)
Aresult
11
00
41 || croak $self->package . " does not export '$fullname'";
42 },
43 2 ▶ merge => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
44 2 my $self = shift;
45 2 my ( $data, $metric, $action, $merge ) = @_;
46 2 my $newmerge = {};
47  
48 2 for my $item ( keys %$merge ) {
49 10 my $value = $merge->{$item};
50 10 ▶ next if $value->isa(Alias);
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa("Exporter::Declare::Export::Alias") 2 8 10
51 8 ▶ next if $data->{$item};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$item} 0 8 8
52 8 $newmerge->{$item} = $value;
53 }
54 2 $self->default_hash_merge( $data, $metric, $action, $newmerge );
55 },
56 1 ▶ list => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 my $self = shift;
58 1 my ($data) = @_;
59 1 return keys %$data;
60 },
61 7 );
62  
63 hash_metric options => (
64 5 ▶ add => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
65 5 my $self = shift;
66 5 my ( $data, $metric, $action, $item ) = @_;
67  
68 5 ▶ croak "'$item' is already a tag, you can't also make it an option."
Branch: 1 branch
BranchTrueFalseTotal
if $self->export_tags_has($item) 0 5 5
69 if $self->export_tags_has($item);
70 5 ▶ croak "'$item' is already an argument, you can't also make it an option."
Branch: 1 branch
BranchTrueFalseTotal
if $self->arguments_has($item) 0 5 5
71 if $self->arguments_has($item);
72  
73 5 $self->default_hash_add( $data, $metric, $action, $item, 1 );
74 },
75 1 ▶ list => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 my $self = shift;
77 1 my ($data) = @_;
78 1 return keys %$data;
79 },
80 7 );
81  
82 hash_metric arguments => (
83 6 ▶ add => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
84 6 my $self = shift;
85 6 my ( $data, $metric, $action, $item ) = @_;
86  
87 6 ▶ croak "'$item' is already a tag, you can't also make it an argument."
Branch: 1 branch
BranchTrueFalseTotal
if $self->export_tags_has($item) 0 6 6
88 if $self->export_tags_has($item);
89 6 ▶ croak "'$item' is already an option, you can't also make it an argument."
Branch: 1 branch
BranchTrueFalseTotal
if $self->options_has($item) 0 6 6
90 if $self->options_has($item);
91  
92 6 $self->default_hash_add( $data, $metric, $action, $item, 1 );
93 },
94 2 ▶ merge => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 2 my $self = shift;
96 2 my ( $data, $metric, $action, $merge ) = @_;
97 2 my $newmerge = {%$merge};
98 2 delete $newmerge->{suffix};
99 2 delete $newmerge->{prefix};
100 2 $self->default_hash_merge( $data, $metric, $action, $newmerge );
101 },
102 1 ▶ list => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
103 1 my $self = shift;
104 1 my ($data) = @_;
105 1 return keys %$data;
106 },
107 7 );
108  
109 lists_metric export_tags => (
110 79 ▶ push => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
111 79 my $self = shift;
112 79 my ( $data, $metric, $action, $item, @args ) = @_;
113  
114 79 ▶ croak "'$item' is a reserved tag, you cannot override it."
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "all" 1 78 79
115 if $item eq 'all';
116 78 ▶ croak "'$item' is already an option, you can't also make it a tag."
Branch: 1 branch
BranchTrueFalseTotal
if $self->options_has($item) 0 78 78
117 if $self->options_has($item);
118 78 ▶ croak "'$item' is already an argument, you can't also make it a tag."
Branch: 1 branch
BranchTrueFalseTotal
if $self->arguments_has($item) 0 78 78
119 if $self->arguments_has($item);
120  
121 78 $self->default_list_push( $data, $metric, $action, $item, @args );
122 },
123 2 ▶ merge => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
124 2 my $self = shift;
125 2 my ( $data, $metric, $action, $merge ) = @_;
126 2 my $newmerge = {};
127 my %aliases = (
128 map {
129 2 my ($name) = (m/^&?(.*)$/);
130 2 ( $name => 1, "&$name" => 1 )
131 2 } @{$merge->{alias}}
132 );
133  
134 2 for my $item ( keys %$merge ) {
135 6 my $values = $merge->{$item};
136 6 $newmerge->{$item} = [grep { !$aliases{$_} } @$values];
137 }
138  
139 2 $self->default_list_merge( $data, $metric, $action, $newmerge );
140 },
141 1 ▶ list => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 1 my $self = shift;
143 1 my ($data) = @_;
144 1 return keys %$data;
145 },
146 7 );
147  
148 19 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
149 19 my $class = shift;
150 19 my $self = $class->SUPER::new(
151 @_,
152 export_tags => {all => [], default => [], alias => []},
153 arguments => {prefix => 1, suffix => 1},
154 );
155 19 $self->add_alias;
156 19 return $self;
157 }
158  
159 2 ▶sub new_from_exporter {
Subroutine new_from_exporter: called CC=5 SCAR=16.1
Pod documented
160 2 my $class = shift;
161 2 my ($exporter) = @_;
162 2 my $self = $class->new($exporter);
163 2 my %seen;
164 2 my ($exports) = $self->get_ref_from_package('@EXPORT');
165 2 my ($export_oks) = $self->get_ref_from_package('@EXPORT_OK');
166 2 my ($tags) = $self->get_ref_from_package('%EXPORT_TAGS');
167 2 $self->exports_add(@$_) for map {
168 7 my ( $ref, $name ) = $self->get_ref_from_package($_);
169  
170 7 ▶ if ( $name =~ m/^\&/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\&/) { } 6 1 7
171 6 Sub->new( $ref, exported_by => $exporter );
172 }
173 else {
174 1 Variable->new( $ref, exported_by => $exporter );
175 }
176 7 [$name, $ref];
177 7 } grep { !$seen{$_}++ } @$exports, @$export_oks;
178 2 ▶ $self->export_tags_push( 'default', @$exports )
Branch: 1 branch
BranchTrueFalseTotal
if @$exports 1 1 2
179 if @$exports;
180 2 $self->export_tags_push( $_, $tags->{$_} ) for keys %$tags;
181 2 return $self;
182 }
183  
184 19 ▶sub add_alias {
Subroutine add_alias: called CC=1 SCAR=0.0
Pod documented
185 19 my $self = shift;
186 19 my $package = $self->package;
187 19 my ($alias) = ( $package =~ m/([^:]+)$/ );
188 19 ▶ $self->exports_add( $alias, Alias->new( sub { $package }, exported_by => $package ) );
Subroutine __ANON__: called CC=1 SCAR=0.0
189 19 $self->export_tags_push( 'alias', $alias );
190 }
191  
192 0 ▶sub is_tag {
Subroutine is_tag: not called CC=2 SCAR=17.9
Pod documented
193 0 my $self = shift;
194 0 my ($name) = @_;
195 0 ▶ return exists $self->export_tags->{$name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->export_tags->{$name} ? : 0 0 0
196 }
197  
198 0 ▶sub is_argument {
Subroutine is_argument: not called CC=2 SCAR=17.9
Pod documented
199 0 my $self = shift;
200 0 my ($name) = @_;
201 0 ▶ return exists $self->arguments->{$name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->arguments->{$name} ? : 0 0 0
202 }
203  
204 0 ▶sub is_option {
Subroutine is_option: not called CC=2 SCAR=17.9
Pod documented
205 0 my $self = shift;
206 0 my ($name) = @_;
207 0 ▶ return exists $self->options->{$name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->options->{$name} ? : 0 0 0
208 }
209  
210 78 ▶sub get_ref_from_package {
Subroutine get_ref_from_package: called CC=8 SCAR=20.9
Pod documented
211 78 my $self = shift;
212 78 my ($item) = @_;
213 7 ▶ use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
214 78 ▶ confess unless $item;
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 78 78
215 78 my ( $type, $name ) = ( $item =~ m/^([\&\@\%\$]?)(.*)$/ );
216 78 ▶ $type ||= '&';
Condition100%2/2
$type ||= "&"
l!l
1563
MC/DC100%1/1
$type ||= "&"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
217 78 my $fullname = "$type$name";
218 78 my $ref = $self->package . '::' . $name;
219  
220 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 78 ▶ return ( \&{$ref}, $fullname ) if !$type || $type eq '&';
Branch: 1 branch
BranchTrueFalseTotal
if not $type or $type eq "&" 64 14 78
Condition66%2/3
not $type or $type eq "&"
l!l&&r!l&&!r
06414
MC/DC50%1/2
not $type or $type eq "&"
not $type $type eq "&"
Truth table66%2/3
not(A) or B
A= not $typeB= $type eq "&"
ABresult
1X1
011
000
222 14 ▶ return ( \${$ref}, $fullname ) if $type eq '$';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "\$" 6 8 14
223 8 ▶ return ( \@{$ref}, $fullname ) if $type eq '@';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "\@" 5 3 8
224 3 ▶ return ( \%{$ref}, $fullname ) if $type eq '%';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "%" 3 0 3
225 0 croak "'$item' cannot be exported";
226 }
227  
228 2 ▶sub reexport {
Subroutine reexport: called CC=2 SCAR=6.9
Pod documented
229 2 my $self = shift;
230 2 my ($exporter) = @_;
231 2 ▶ my $meta =
Branch: 1 branch
BranchTrueFalseTotal
$exporter->can('export_meta') ? : 1 1 2
232 $exporter->can('export_meta')
233 ? $exporter->export_meta()
234 : __PACKAGE__->new_from_exporter($exporter);
235 2 $self->merge($meta);
236 }
237  
238 7 1;
239  
240 =head1 NAME
241  
242 Exporter::Declare::Meta - The meta object which stores meta-data for all
243 exporters.
244  
245 =head1 DESCRIPTION
246  
247 All classes that use Exporter::Declare have an associated Meta object. Meta
248 objects track available exports, tags, and options.
249  
250 =head1 METHODS
251  
252 =over 4
253  
254 =item $class->new( $package )
255  
256 Created a meta object for the specified package. Also injects the export_meta()
257 sub into the package namespace that returns the generated meta object.
258  
259 =item $class->new_from_exporter( $package )
260  
261 Create a meta object for a package that already uses Exporter.pm. This will not
262 turn the class into an Exporter::Declare package, but it will create a meta
263 object and export_meta() method on it. This si primarily used for reexport
264 purposes.
265  
266 =item $package = $meta->package()
267  
268 Get the name of the package with which the meta object is associated.
269  
270 =item $meta->add_alias()
271  
272 Usually called at construction to add a package alias function to the exports.
273  
274 =item $meta->add_export( $name, $ref )
275  
276 Add an export, name should be the item name with sigil (assumed to be sub if
277 there is no sigil). $ref should be a ref blessed as an
278 L<Exporter::Declare::Export> subclass.
279  
280 =item $meta->get_export( $name )
281  
282 Retrieve the L<Exporter::Declare::Export> object by name. Name should be the
283 item name with sigil, assumed to be sub when sigil is missing.
284  
285 =item $meta->export_tags_push( $name, @items )
286  
287 Add @items to the specified tag. Tag will be created if it does not already
288 exist. $name should be the tag name B<WITHOUT> -/: prefix.
289  
290 =item @list = $meta->export_tags_get( $name )
291  
292 Get the list of items associated with the specified tag. $name should be the
293 tag name B<WITHOUT> -/: prefix.
294  
295 =item @list = $meta->export_tags_list()
296  
297 Get a list of all export tags.
298  
299 =item $bool = $meta->is_tag( $name )
300  
301 Check if a tag with the given name exists. $name should be the tag name
302 B<WITHOUT> -/: prefix.
303  
304 =item $meta->options_add( $name )
305  
306 Add import options by name. These will be boolean options that take no
307 arguments.
308  
309 =item my @list = $meta->options_list()
310  
311 =item $meta->arguments_add( $name )
312  
313 Add import options that slurp in the next argument as a value.
314  
315 =item $bool = $meta->is_option( $name )
316  
317 Check if the specified name is an option.
318  
319 =item $bool = $meta->is_argument( $name )
320  
321 Check if the specified name is an option that takes an argument.
322  
323 =item $meta->add_parser( $name, sub { ... })
324  
325 Add a parser sub that should be associated with exports via L<Devel::Declare>
326  
327 =item $meta->get_parser( $name )
328  
329 Get a parser by name.
330  
331 =item $ref = $meta->get_ref_from_package( $item )
332  
333 Returns a reference to a specific package variable or sub.
334  
335 =item $meta->reexport( $package )
336  
337 Re-export the exports in the provided package. Package may be an
338 L<Exporter::Declare> based package or an L<Exporter> based package.
339  
340 =item $meta->merge( $meta2 )
341  
342 Merge-in the exports and tags of the second meta object.
343  
344 =back
345  
346 =head1 AUTHORS
347  
348 Chad Granum L<exodist7@gmail.com>
349  
350 =head1 COPYRIGHT
351  
352 Copyright (C) 2010 Chad Granum
353  
354 Exporter-Declare is free software; Standard perl licence.
355  
356 Exporter-Declare is distributed in the hope that it will be useful, but
357 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
358 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
« Declare.pm ↑ Summary Declare/Export.pm »