Revert "MXSC must be used by Moose classes, nothing else"
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor.pm
1 package MooseX::StrictConstructor;
2
3 use strict;
4 use warnings;
5
6 use Moose 0.94 ();
7 use Moose::Exporter;
8 use Moose::Util::MetaRole;
9
10 {
11     my %class_meta = ( class => ['MooseX::StrictConstructor::Trait::Class'] );
12
13
14     if ( $Moose::VERSION < 1.9900 ) {
15         require MooseX::StrictConstructor::Trait::Method::Constructor;
16         $class_meta{constructor}
17             = ['MooseX::StrictConstructor::Trait::Method::Constructor'];
18     }
19
20     Moose::Exporter->setup_import_methods(
21         class_metaroles => \%class_meta,
22     );
23 }
24
25 1;
26
27 # ABSTRACT: Make your object constructors blow up on unknown attributes
28
29 __END__
30
31 =pod
32
33 =head1 SYNOPSIS
34
35     package My::Class;
36
37     use Moose;
38     use MooseX::StrictConstructor;
39
40     has 'size' => ...;
41
42     # then later ...
43
44     # this blows up because color is not a known attribute
45     My::Class->new( size => 5, color => 'blue' );
46
47 =head1 DESCRIPTION
48
49 Simply loading this module makes your constructors "strict". If your
50 constructor is called with an attribute init argument that your class
51 does not declare, then it calls C<Moose->throw_error()>. This is a great way
52 to catch small typos.
53
54 =head2 Subverting Strictness
55
56 You may find yourself wanting to have your constructor accept a
57 parameter which does not correspond to an attribute.
58
59 In that case, you'll probably also be writing a C<BUILD()> or
60 C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
61 method, you can simply make sure that this parameter is not included
62 in the hash reference you return. Otherwise, in a C<BUILD()> method,
63 you can delete it from the hash reference of parameters.
64
65   sub BUILD {
66       my $self   = shift;
67       my $params = shift;
68
69       if ( delete $params->{do_something} ) {
70           ...
71       }
72   }
73
74 =head1 BUGS
75
76 Please report any bugs or feature requests to
77 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
78 interface at L<http://rt.cpan.org>.  I will be notified, and then
79 you'll automatically be notified of progress on your bug as I make
80 changes.
81
82 =cut