File Coverage

blib/lib/MooseX/Types/Util.pm
Criterion Covered Total %
statement 25 27 92.5
branch 5 6 83.3
condition 2 3 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 41 45 91.1


line stmt bran cond sub pod time code
1 17     17   24517 use warnings;
  17         35  
  17         644  
2 17     17   100 use strict;
  17         44  
  17         1307  
3             package MooseX::Types::Util;
4             # ABSTRACT: Common utility functions for the distribution
5             $MooseX::Types::Util::VERSION = '0.45';
6 17     17   106 use Scalar::Util 'blessed';
  17         29  
  17         1157  
7 17     17   130 use base 'Exporter';
  17         32  
  17         2060  
8 17     17   1170 use namespace::autoclean;
  17         22332  
  17         182  
9              
10             #pod =head1 DESCRIPTION
11             #pod
12             #pod This package the exportable functions that many parts in
13             #pod L<MooseX::Types> might need.
14             #pod
15             #pod =cut
16              
17             our @EXPORT_OK = qw( filter_tags has_available_type_export );
18              
19             #pod =head1 FUNCTIONS
20             #pod
21             #pod =head2 filter_tags
22             #pod
23             #pod Takes a list and returns two references. The first is a hash reference
24             #pod containing the tags as keys and the number of their appearance as values.
25             #pod The second is an array reference containing all other elements.
26             #pod
27             #pod =cut
28              
29             sub filter_tags {
30 20     20 1 61 my (@list) = @_;
31 20         42 my (%tags, @other);
32 20         57 for (@list) {
33 57 50       180 if (/^:(.*)$/) {
34 0         0 $tags{ $1 }++;
35 0         0 next;
36             }
37 57         125 push @other, $_;
38             }
39 20         101 return \%tags, \@other;
40             }
41              
42             #pod =head2 has_available_type_export
43             #pod
44             #pod TypeConstraint | Undef = has_available_type_export($package, $name);
45             #pod
46             #pod This function allows you to introspect if a given type export is available
47             #pod I<at this point in time>. This means that the C<$package> must have imported
48             #pod a type constraint with the name C<$name>, and it must be still in its symbol
49             #pod table.
50             #pod
51             #pod Two arguments are expected:
52             #pod
53             #pod =over 4
54             #pod
55             #pod =item $package
56             #pod
57             #pod The name of the package to introspect.
58             #pod
59             #pod =item $name
60             #pod
61             #pod The name of the type export to introspect.
62             #pod
63             #pod =back
64             #pod
65             #pod B<Note> that the C<$name> is the I<exported> name of the type, not the declared
66             #pod one. This means that if you use L<Sub::Exporter>s functionality to rename an import
67             #pod like this:
68             #pod
69             #pod use MyTypes Str => { -as => 'MyStr' };
70             #pod
71             #pod you would have to introspect this type like this:
72             #pod
73             #pod has_available_type_export $package, 'MyStr';
74             #pod
75             #pod The return value will be either the type constraint that belongs to the export
76             #pod or an undefined value.
77             #pod
78             #pod =cut
79              
80             sub has_available_type_export {
81 13     13 1 3509 my ($package, $name) = @_;
82              
83 13 100       117 my $sub = $package->can($name)
84             or return undef;
85              
86             return undef
87 5 100 66     44 unless blessed $sub && $sub->isa('MooseX::Types::EXPORTED_TYPE_CONSTRAINT');
88              
89 4         15 return $sub->();
90             }
91              
92             #pod =head1 SEE ALSO
93             #pod
94             #pod L<MooseX::Types::Moose>, L<Exporter>
95             #pod
96             #pod =cut
97              
98             1;
99              
100             __END__
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             MooseX::Types::Util - Common utility functions for the distribution
109              
110             =head1 VERSION
111              
112             version 0.45
113              
114             =head1 DESCRIPTION
115              
116             This package the exportable functions that many parts in
117             L<MooseX::Types> might need.
118              
119             =head1 FUNCTIONS
120              
121             =head2 filter_tags
122              
123             Takes a list and returns two references. The first is a hash reference
124             containing the tags as keys and the number of their appearance as values.
125             The second is an array reference containing all other elements.
126              
127             =head2 has_available_type_export
128              
129             TypeConstraint | Undef = has_available_type_export($package, $name);
130              
131             This function allows you to introspect if a given type export is available
132             I<at this point in time>. This means that the C<$package> must have imported
133             a type constraint with the name C<$name>, and it must be still in its symbol
134             table.
135              
136             Two arguments are expected:
137              
138             =over 4
139              
140             =item $package
141              
142             The name of the package to introspect.
143              
144             =item $name
145              
146             The name of the type export to introspect.
147              
148             =back
149              
150             B<Note> that the C<$name> is the I<exported> name of the type, not the declared
151             one. This means that if you use L<Sub::Exporter>s functionality to rename an import
152             like this:
153              
154             use MyTypes Str => { -as => 'MyStr' };
155              
156             you would have to introspect this type like this:
157              
158             has_available_type_export $package, 'MyStr';
159              
160             The return value will be either the type constraint that belongs to the export
161             or an undefined value.
162              
163             =head1 SEE ALSO
164              
165             L<MooseX::Types::Moose>, L<Exporter>
166              
167             =head1 AUTHOR
168              
169             Robert "phaylon" Sedlacek <rs@474.at>
170              
171             =head1 COPYRIGHT AND LICENSE
172              
173             This software is copyright (c) 2007 by Robert "phaylon" Sedlacek.
174              
175             This is free software; you can redistribute it and/or modify it under
176             the same terms as the Perl 5 programming language system itself.
177              
178             =cut