bump version to 0.26
[gitmo/MooseX-Types.git] / lib / MooseX / Types / Util.pm
CommitLineData
52d358e2 1package MooseX::Types::Util;
6a1db9c7 2our $VERSION = "0.26";
e211870f 3
4=head1 NAME
5
52d358e2 6MooseX::Types::Util - Common utility functions for the module
e211870f 7
8=cut
9
10use warnings;
11use strict;
be7d5c3e 12use Scalar::Util 'blessed';
e211870f 13
14use base 'Exporter';
15
16=head1 DESCRIPTION
17
18This package the exportable functions that many parts in
52d358e2 19L<MooseX::Types> might need.
e211870f 20
21=cut
22
5885c4f4 23our @EXPORT_OK = qw( filter_tags has_available_type_export );
e211870f 24
25=head1 FUNCTIONS
26
27=head2 filter_tags
28
29Takes a list and returns two references. The first is a hash reference
30containing the tags as keys and the number of their appearance as values.
31The second is an array reference containing all other elements.
32
33=cut
34
35sub 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
52This function allows you to introspect if a given type export is available
53I<at this point in time>. This means that the C<$package> must have imported
54a typeconstraint with the name C<$name>, and it must be still in its symbol
55table.
56
57Two arguments are expected:
58
59=over 4
60
61=item $package
62
63The name of the package to introspect.
64
65=item $name
66
67The name of the type export to introspect.
68
69=back
70
71B<Note> that the C<$name> is the I<exported> name of the type, not the declared
72one. This means that if you use L<Sub::Exporter>s functionality to rename an import
73like this:
74
75 use MyTypes Str => { -as => 'MyStr' };
76
77you would have to introspect this type like this:
78
79 has_available_type_export $package, 'MyStr';
80
81The return value will be either the type constraint that belongs to the export
82or an undefined value.
83
84=cut
85
86sub 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 100L<MooseX::Types::Moose>, L<Exporter>
e211870f 101
b55332a8 102=head1 AUTHOR
e211870f 103
b55332a8 104See L<MooseX::Types/AUTHOR>.
e211870f 105
106=head1 LICENSE
107
108This program is free software; you can redistribute it and/or modify
109it under the same terms as perl itself.
110
111=cut
112
1131;