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