X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FTypes%2FUtil.pm;h=ea2c35c7e97f7791abe71cc337f79817c6f2a742;hb=e9dc30af81ed775a6ac0a0f0c1daad021e5569f6;hp=5bd5bcf8fb7b58773ecf798c8b3f884e448641f7;hpb=52d358e2cf004a8b632b655832f7e9101db3c4dc;p=gitmo%2FMooseX-Types.git diff --git a/lib/MooseX/Types/Util.pm b/lib/MooseX/Types/Util.pm index 5bd5bcf..ea2c35c 100644 --- a/lib/MooseX/Types/Util.pm +++ b/lib/MooseX/Types/Util.pm @@ -1,24 +1,21 @@ package MooseX::Types::Util; -=head1 NAME - -MooseX::Types::Util - Common utility functions for the module - -=cut +#ABSTRACT: Common utility functions for the distribution use warnings; use strict; +use Scalar::Util 'blessed'; use base 'Exporter'; =head1 DESCRIPTION -This package the exportable functions that many parts in +This package the exportable functions that many parts in L might need. =cut -our @EXPORT_OK = qw( filter_tags ); +our @EXPORT_OK = qw( filter_tags has_available_type_export ); =head1 FUNCTIONS @@ -43,14 +40,59 @@ sub filter_tags { return \%tags, \@other; } -=head1 SEE ALSO +=head2 has_available_type_export -L, L + TypeConstraint | Undef = has_available_type_export($package, $name); + +This function allows you to introspect if a given type export is available +I. This means that the C<$package> must have imported +a typeconstraint with the name C<$name>, and it must be still in its symbol +table. + +Two arguments are expected: + +=over 4 + +=item $package + +The name of the package to introspect. + +=item $name + +The name of the type export to introspect. + +=back + +B that the C<$name> is the I name of the type, not the declared +one. This means that if you use Ls functionality to rename an import +like this: -=head1 AUTHOR AND COPYRIGHT + use MyTypes Str => { -as => 'MyStr' }; -Robert 'phaylon' Sedlacek Crs@474.atE>, with many thanks to -the C<#moose> cabal on C. +you would have to introspect this type like this: + + has_available_type_export $package, 'MyStr'; + +The return value will be either the type constraint that belongs to the export +or an undefined value. + +=cut + +sub has_available_type_export { + my ($package, $name) = @_; + + my $sub = $package->can($name) + or return undef; + + return undef + unless blessed $sub && $sub->isa('MooseX::Types::EXPORTED_TYPE_CONSTRAINT'); + + return $sub->(); +} + +=head1 SEE ALSO + +L, L =head1 LICENSE