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