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