reorganize author/copyright sections at the request of Debian packagers
[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
48Moose::Exporter->setup_import_methods(
49 with_caller => [ @exports, 'class_type', 'role_type' ]
50);
51
52for my $export (@exports) {
53 no strict 'refs';
54
55 *{$export} = sub {
56 my $caller = shift;
57
58 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
59
60 carp $StringFoundMsg
61 unless ref($_[0]) ||
62 $_[0] =~ /\b::\b/ || # qualified type
63 $caller->get_registered_class_type($_[0]) ||
64 $caller->get_registered_role_type($_[0]);
65
66 goto &{"Moose::Util::TypeConstraints::$export"};
67 }
68}
69
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
b55332a8 90=head1 AUTHOR
91
92See L<MooseX::Types/AUTHOR>.
93
ca9d7442 94=head1 LICENSE
95
96This program is free software; you can redistribute it and/or modify
97it under the same terms as perl itself.
98
99=cut
100
1011;