1package MooseX::Types::CheckedUtilExports;
2# ABSTRACT: Wrap L<Moose::Util::TypeConstraints> to be safer for L<MooseX::Types>
3
4our $VERSION = '0.50';
5
6use strict;
7use warnings;
8use Moose::Util::TypeConstraints ();
9use Moose::Exporter;
10use Carp 'carp';
11use Sub::Install;
12use namespace::autoclean;
13
14my $StringFoundMsg =
15q{WARNING: String found where Type expected (did you use a => instead of a , ?)};
16
17my @exports = qw/type subtype maybe_type duck_type enum coerce from as/;
18
19#pod =head1 DESCRIPTION
20#pod
21#pod Prevents errors like:
22#pod
23#pod     subtype Foo =>
24#pod     ...
25#pod
26#pod Which should be written as:
27#pod
28#pod     subtype Foo,
29#pod     ...
30#pod
31#pod When using L<MooseX::Types>. Exported by that module.
32#pod
33#pod Exports checked versions of the following subs:
34#pod
35#pod C<type> C<subtype> C<maybe_type> C<duck_type> C<enum> C<coerce> C<from> C<as>
36#pod
37#pod While C<class_type> and C<role_type> will also register the type in the library.
38#pod
39#pod From L<Moose::Util::TypeConstraints>. See that module for syntax.
40#pod
41#pod =for Pod::Coverage class_type role_type
42#pod
43#pod =cut
44
45for my $export (@exports) {
46    no strict 'refs';
47
48    Sub::Install::install_sub({
49      into => __PACKAGE__,
50      as   => $export,
51      code => 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}
66
67Moose::Exporter->setup_import_methods(
68    with_caller => [ @exports, 'class_type', 'role_type' ]
69);
70
71sub class_type {
72    my $caller = shift;
73
74    $caller->register_class_type(
75        Moose::Util::TypeConstraints::class_type(@_)
76    );
77}
78
79sub role_type ($;$) {
80    my ($caller, $name, $opts) = @_;
81
82    $caller->register_role_type(
83        Moose::Util::TypeConstraints::role_type($name, $opts)
84    );
85}
86
87#pod =head1 SEE ALSO
88#pod
89#pod L<MooseX::Types>
90#pod
91#pod =cut
92
931;
94
95__END__
96
97=pod
98
99=encoding UTF-8
100
101=head1 NAME
102
103MooseX::Types::CheckedUtilExports - Wrap L<Moose::Util::TypeConstraints> to be safer for L<MooseX::Types>
104
105=head1 VERSION
106
107version 0.50
108
109=head1 DESCRIPTION
110
111Prevents errors like:
112
113    subtype Foo =>
114    ...
115
116Which should be written as:
117
118    subtype Foo,
119    ...
120
121When using L<MooseX::Types>. Exported by that module.
122
123Exports checked versions of the following subs:
124
125C<type> C<subtype> C<maybe_type> C<duck_type> C<enum> C<coerce> C<from> C<as>
126
127While C<class_type> and C<role_type> will also register the type in the library.
128
129From L<Moose::Util::TypeConstraints>. See that module for syntax.
130
131=for Pod::Coverage class_type role_type
132
133=head1 SEE ALSO
134
135L<MooseX::Types>
136
137=head1 SUPPORT
138
139Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types>
140(or L<bug-MooseX-Types@rt.cpan.org|mailto:bug-MooseX-Types@rt.cpan.org>).
141
142There is also a mailing list available for users of this distribution, at
143L<http://lists.perl.org/list/moose.html>.
144
145There is also an irc channel available for users of this distribution, at
146L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
147
148=head1 AUTHOR
149
150Robert "phaylon" Sedlacek <rs@474.at>
151
152=head1 COPYRIGHT AND LICENCE
153
154This software is copyright (c) 2007 by Robert "phaylon" Sedlacek.
155
156This is free software; you can redistribute it and/or modify it under
157the same terms as the Perl 5 programming language system itself.
158
159=cut
160