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