Commit | Line | Data |
52d358e2 |
1 | package MooseX::Types::Util; |
f5559e1c |
2 | our $VERSION = "0.25"; |
e211870f |
3 | |
4 | =head1 NAME |
5 | |
52d358e2 |
6 | MooseX::Types::Util - Common utility functions for the module |
e211870f |
7 | |
8 | =cut |
9 | |
10 | use warnings; |
11 | use strict; |
be7d5c3e |
12 | use Scalar::Util 'blessed'; |
e211870f |
13 | |
14 | use base 'Exporter'; |
15 | |
16 | =head1 DESCRIPTION |
17 | |
18 | This package the exportable functions that many parts in |
52d358e2 |
19 | L<MooseX::Types> might need. |
e211870f |
20 | |
21 | =cut |
22 | |
5885c4f4 |
23 | our @EXPORT_OK = qw( filter_tags has_available_type_export ); |
e211870f |
24 | |
25 | =head1 FUNCTIONS |
26 | |
27 | =head2 filter_tags |
28 | |
29 | Takes a list and returns two references. The first is a hash reference |
30 | containing the tags as keys and the number of their appearance as values. |
31 | The second is an array reference containing all other elements. |
32 | |
33 | =cut |
34 | |
35 | sub filter_tags { |
36 | my (@list) = @_; |
37 | my (%tags, @other); |
38 | for (@list) { |
39 | if (/^:(.*)$/) { |
40 | $tags{ $1 }++; |
41 | next; |
42 | } |
43 | push @other, $_; |
44 | } |
45 | return \%tags, \@other; |
46 | } |
47 | |
5885c4f4 |
48 | =head2 has_available_type_export |
49 | |
50 | TypeConstraint | Undef = has_available_type_export($package, $name); |
51 | |
52 | This function allows you to introspect if a given type export is available |
53 | I<at this point in time>. This means that the C<$package> must have imported |
54 | a typeconstraint with the name C<$name>, and it must be still in its symbol |
55 | table. |
56 | |
57 | Two arguments are expected: |
58 | |
59 | =over 4 |
60 | |
61 | =item $package |
62 | |
63 | The name of the package to introspect. |
64 | |
65 | =item $name |
66 | |
67 | The name of the type export to introspect. |
68 | |
69 | =back |
70 | |
71 | B<Note> that the C<$name> is the I<exported> name of the type, not the declared |
72 | one. This means that if you use L<Sub::Exporter>s functionality to rename an import |
73 | like this: |
74 | |
75 | use MyTypes Str => { -as => 'MyStr' }; |
76 | |
77 | you would have to introspect this type like this: |
78 | |
79 | has_available_type_export $package, 'MyStr'; |
80 | |
81 | The return value will be either the type constraint that belongs to the export |
82 | or an undefined value. |
83 | |
84 | =cut |
85 | |
86 | sub has_available_type_export { |
87 | my ($package, $name) = @_; |
88 | |
89 | my $sub = $package->can($name) |
90 | or return undef; |
91 | |
92 | return undef |
be7d5c3e |
93 | unless blessed $sub && $sub->isa('MooseX::Types::EXPORTED_TYPE_CONSTRAINT'); |
5885c4f4 |
94 | |
95 | return $sub->(); |
96 | } |
97 | |
e211870f |
98 | =head1 SEE ALSO |
99 | |
52d358e2 |
100 | L<MooseX::Types::Moose>, L<Exporter> |
e211870f |
101 | |
b55332a8 |
102 | =head1 AUTHOR |
e211870f |
103 | |
b55332a8 |
104 | See L<MooseX::Types/AUTHOR>. |
e211870f |
105 | |
106 | =head1 LICENSE |
107 | |
108 | This program is free software; you can redistribute it and/or modify |
109 | it under the same terms as perl itself. |
110 | |
111 | =cut |
112 | |
113 | 1; |