typo fixes
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox.pm
CommitLineData
5f654d8e 1
2package Moose::Autobox;
ca2e2c86 3use 5.006;
5f654d8e 4use strict;
5use warnings;
6
31d40d73 7use Carp qw(confess);
5f654d8e 8use Scalar::Util ();
ea4e64bf 9use Moose::Util ();
5f654d8e 10
a1f00ce9 11our $VERSION = '0.13';
7dad2765 12
f3e571b2 13use parent 'autobox';
7dad2765 14
19b62758 15use Moose::Autobox::Undef;
16
8937074a 17sub import {
7dad2765 18 (shift)->SUPER::import(
19 DEFAULT => 'Moose::Autobox::',
20 UNDEF => 'Moose::Autobox::Undef',
21 );
22}
7dad2765 23
244bd352 24sub mixin_additional_role {
25 my ($class, $type, $role) = @_;
26 ($type =~ /SCALAR|ARRAY|HASH|CODE/)
27 || confess "Can only add additional roles to SCALAR, ARRAY, HASH or CODE";
1401ddb2 28 Moose::Util::apply_all_roles(('Moose::Autobox::' . $type)->meta, ($role));
244bd352 29}
7dad2765 30
244bd352 31{
32
33 package Moose::Autobox::SCALAR;
ee6bd664 34
35 use Moose::Autobox::Scalar;
36
37 use metaclass 'Moose::Meta::Class';
38
1401ddb2 39 Moose::Util::apply_all_roles(__PACKAGE__->meta, ('Moose::Autobox::Scalar'));
244bd352 40
41 *does = \&Moose::Object::does;
42
43 package Moose::Autobox::ARRAY;
ee6bd664 44
45 use Moose::Autobox::Array;
46
47 use metaclass 'Moose::Meta::Class';
48
1401ddb2 49 Moose::Util::apply_all_roles(__PACKAGE__->meta, ('Moose::Autobox::Array'));
244bd352 50
51 *does = \&Moose::Object::does;
52
53 package Moose::Autobox::HASH;
ee6bd664 54
55 use Moose::Autobox::Hash;
56
57 use metaclass 'Moose::Meta::Class';
58
1401ddb2 59 Moose::Util::apply_all_roles(__PACKAGE__->meta, ('Moose::Autobox::Hash'));
244bd352 60
61 *does = \&Moose::Object::does;
62
63 package Moose::Autobox::CODE;
ee6bd664 64
65 use Moose::Autobox::Code;
66
67 use metaclass 'Moose::Meta::Class';
68
1401ddb2 69 Moose::Util::apply_all_roles(__PACKAGE__->meta, ('Moose::Autobox::Code'));
244bd352 70
71 *does = \&Moose::Object::does;
72
73}
7dad2765 74
5f654d8e 751;
76
77__END__
78
79=pod
80
81=head1 NAME
82
ea4e64bf 83Moose::Autobox - Autoboxed wrappers for Native Perl datatypes
5f654d8e 84
85=head1 SYNOPOSIS
86
39894c95 87 use Moose::Autobox;
31d40d73 88
8937074a 89 print 'Print squares from 1 to 10 : ';
90 print [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', ');
91
5f654d8e 92=head1 DESCRIPTION
93
8937074a 94Moose::Autobox provides an implementation of SCALAR, ARRAY, HASH
95& CODE for use with L<autobox>. It does this using a hierarchy of
96roles in a manner similar to what Perl 6 I<might> do. This module,
97like L<Class::MOP> and L<Moose>, was inspired by my work on the
98Perl 6 Object Space, and the 'core types' implemented there.
99
100=head2 A quick word about autobox
101
102The L<autobox> module provides the ability for calling 'methods'
103on normal Perl values like Scalars, Arrays, Hashes and Code
104references. This gives the illusion that Perl's types are first-class
105objects. However, this is only an illusion, albeit a very nice one.
106I created this module because L<autobox> itself does not actually
107provide an implementation for the Perl types but instead only provides
108the 'hooks' for others to add implementation too.
109
110=head2 Is this for real? or just play?
111
ea4e64bf 112Several people are using this module in serious applications and
113it seems to be quite stable. The underlying technologies of L<autobox>
114and L<Moose::Role> are also considered stable. There is some performance
a34c4a98 115hit, but as I am fond of saying, nothing in life is free. Note that this hit
116only applies to the I<use> of methods on native Perl values, not the mere act
117of loading this module in your namespace.
118
119If you have any questions regarding this module, either email me, or stop by
120#moose on irc.perl.org and ask around.
8937074a 121
7f9d7a3a 122=head2 Adding additional methods
123
124B<Moose::Autobox> asks L<autobox> to use the B<Moose::Autobox::*> namespace
125prefix so as to avoid stepping on the toes of other L<autobox> modules. This
126means that if you want to add methods to a particular perl type
127(i.e. - monkeypatch), then you must do this:
128
129 sub Moose::Autobox::SCALAR::bar { 42 }
130
131instead of this:
132
e1521af2 133 sub SCALAR::bar { 42 }
7f9d7a3a 134
135as you would with vanilla autobox.
136
244bd352 137=head1 METHODS
138
139=over 4
140
141=item B<mixin_additional_role ($type, $role)>
142
ca88ba89 143This will mixin an additional C<$role> into a certain C<$type>. The
244bd352 144types can be SCALAR, ARRAY, HASH or CODE.
145
146This can be used to add additional methods to the types, see the
147F<examples/units/> directory for some examples.
148
149=back
1972aa1b 150
151=head1 TODO
152
153=over 4
154
155=item More docs
156
157=item More tests
158
159=back
31d40d73 160
5f654d8e 161=head1 BUGS
162
163All complex software has bugs lurking in it, and this module is no
164exception. If you find a bug please either email me, or add the bug
165to cpan-RT.
166
167=head1 AUTHOR
168
169Stevan Little E<lt>stevan@iinteractive.comE<gt>
170
b3cb7038 171B<with contributions from:>
172
173Anders (Debolaz) Nor Berle
174
175Matt (mst) Trout
176
177renormalist
178
5f654d8e 179=head1 COPYRIGHT AND LICENSE
180
ea4e64bf 181Copyright 2006-2008 by Infinity Interactive, Inc.
5f654d8e 182
183L<http://www.iinteractive.com>
184
185This library is free software; you can redistribute it and/or modify
186it under the same terms as Perl itself.
187
5f654d8e 188=cut