switching over to dzil
[gitmo/MooseX-Types.git] / lib / MooseX / Types / CheckedUtilExports.pm
CommitLineData
ca9d7442 1=head1 NAME
2
3MooseX::Types::CheckedUtilExports - Wrap L<Moose::Util::TypeConstraints> to be
4safer for L<MooseX::Types>
5
6=cut
7
8package MooseX::Types::CheckedUtilExports;
9
10use strict;
11use warnings;
12use Moose::Util::TypeConstraints ();
13use Moose::Exporter;
14use Sub::Name;
15use Carp;
16
17use namespace::clean -except => 'meta';
18
19my $StringFoundMsg =
20q{WARNING: String found where Type expected (did you use a => instead of a , ?)};
21
22my @exports = qw/type subtype maybe_type duck_type enum coerce from as/;
23
24=head1 DESCRIPTION
25
26Prevents errors like:
27
28 subtype Foo =>
29 ...
30
31Which should be written as:
32
33 subtype Foo,
34 ...
35
36When using L<MooseX::Types>. Exported by that module.
37
38Exports checked versions of the following subs:
39
40C<type> C<subtype> C<maybe_type> C<duck_type> C<enum> C<coerce> C<from> C<as>
41
42While C<class_type> and C<role_type> will also register the type in the library.
43
44From L<Moose::Util::TypeConstraints>. See that module for syntax.
45
46=cut
47
ca9d7442 48for my $export (@exports) {
49 no strict 'refs';
50
51 *{$export} = sub {
52 my $caller = shift;
53
54 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
55
56 carp $StringFoundMsg
57 unless ref($_[0]) ||
58 $_[0] =~ /\b::\b/ || # qualified type
59 $caller->get_registered_class_type($_[0]) ||
60 $caller->get_registered_role_type($_[0]);
61
62 goto &{"Moose::Util::TypeConstraints::$export"};
63 }
64}
65
f8779bc5 66Moose::Exporter->setup_import_methods(
67 with_caller => [ @exports, 'class_type', 'role_type' ]
68);
69
ca9d7442 70sub class_type {
71 my $caller = shift;
72
73 $caller->register_class_type(
74 Moose::Util::TypeConstraints::class_type(@_)
75 );
76}
77
78sub role_type ($;$) {
79 my ($caller, $name, $opts) = @_;
80
81 $caller->register_role_type(
82 Moose::Util::TypeConstraints::role_type($name, $opts)
83 );
84}
85
86=head1 SEE ALSO
87
88L<MooseX::Types>
89
90=head1 LICENSE
91
92This program is free software; you can redistribute it and/or modify
93it under the same terms as perl itself.
94
95=cut
96
971;