Commit | Line | Data |
5f654d8e |
1 | |
2 | package Moose::Autobox; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
31d40d73 |
7 | use Carp qw(confess); |
5f654d8e |
8 | use Scalar::Util (); |
9 | |
1401ddb2 |
10 | our $VERSION = '0.06'; |
7dad2765 |
11 | |
12 | use base 'autobox'; |
13 | |
19b62758 |
14 | use Moose::Autobox::Undef; |
15 | |
8937074a |
16 | sub import { |
7dad2765 |
17 | (shift)->SUPER::import( |
18 | DEFAULT => 'Moose::Autobox::', |
19 | UNDEF => 'Moose::Autobox::Undef', |
20 | ); |
21 | } |
7dad2765 |
22 | |
244bd352 |
23 | sub mixin_additional_role { |
24 | my ($class, $type, $role) = @_; |
25 | ($type =~ /SCALAR|ARRAY|HASH|CODE/) |
26 | || confess "Can only add additional roles to SCALAR, ARRAY, HASH or CODE"; |
1401ddb2 |
27 | Moose::Util::apply_all_roles(('Moose::Autobox::' . $type)->meta, ($role)); |
244bd352 |
28 | } |
7dad2765 |
29 | |
244bd352 |
30 | { |
31 | |
32 | package Moose::Autobox::SCALAR; |
ee6bd664 |
33 | |
34 | use Moose::Autobox::Scalar; |
35 | |
36 | use metaclass 'Moose::Meta::Class'; |
37 | |
1401ddb2 |
38 | Moose::Util::apply_all_roles(__PACKAGE__->meta, ('Moose::Autobox::Scalar')); |
244bd352 |
39 | |
40 | *does = \&Moose::Object::does; |
41 | |
42 | package Moose::Autobox::ARRAY; |
ee6bd664 |
43 | |
44 | use Moose::Autobox::Array; |
45 | |
46 | use metaclass 'Moose::Meta::Class'; |
47 | |
1401ddb2 |
48 | Moose::Util::apply_all_roles(__PACKAGE__->meta, ('Moose::Autobox::Array')); |
244bd352 |
49 | |
50 | *does = \&Moose::Object::does; |
51 | |
52 | package Moose::Autobox::HASH; |
ee6bd664 |
53 | |
54 | use Moose::Autobox::Hash; |
55 | |
56 | use metaclass 'Moose::Meta::Class'; |
57 | |
1401ddb2 |
58 | Moose::Util::apply_all_roles(__PACKAGE__->meta, ('Moose::Autobox::Hash')); |
244bd352 |
59 | |
60 | *does = \&Moose::Object::does; |
61 | |
62 | package Moose::Autobox::CODE; |
ee6bd664 |
63 | |
64 | use Moose::Autobox::Code; |
65 | |
66 | use metaclass 'Moose::Meta::Class'; |
67 | |
1401ddb2 |
68 | Moose::Util::apply_all_roles(__PACKAGE__->meta, ('Moose::Autobox::Code')); |
244bd352 |
69 | |
70 | *does = \&Moose::Object::does; |
71 | |
72 | } |
7dad2765 |
73 | |
5f654d8e |
74 | 1; |
75 | |
76 | __END__ |
77 | |
78 | =pod |
79 | |
80 | =head1 NAME |
81 | |
19b62758 |
82 | Moose::Autobox - Ruby ain't got nothin on us |
5f654d8e |
83 | |
84 | =head1 SYNOPOSIS |
85 | |
39894c95 |
86 | use Moose::Autobox; |
31d40d73 |
87 | |
8937074a |
88 | print 'Print squares from 1 to 10 : '; |
89 | print [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', '); |
90 | |
91 | =head1 CAVEAT |
92 | |
93 | First, a warning. |
94 | |
95 | This module is very very very very very very very experimental. It |
96 | makes use of a very experimental module (L<autobox>) and uses some |
97 | shiney new technology (L<Moose::Role>) to accomplish it's goals. |
98 | |
99 | Use this at your own risk. If it breaks the lamp in the living room |
100 | and your mother yells at you, don't come complaining to me. |
39894c95 |
101 | |
1972aa1b |
102 | Also, as this is so experimental, it's API should not be considered |
103 | to be stable. It could very well change in radical ways. |
104 | |
5f654d8e |
105 | =head1 DESCRIPTION |
106 | |
8937074a |
107 | Moose::Autobox provides an implementation of SCALAR, ARRAY, HASH |
108 | & CODE for use with L<autobox>. It does this using a hierarchy of |
109 | roles in a manner similar to what Perl 6 I<might> do. This module, |
110 | like L<Class::MOP> and L<Moose>, was inspired by my work on the |
111 | Perl 6 Object Space, and the 'core types' implemented there. |
112 | |
113 | =head2 A quick word about autobox |
114 | |
115 | The L<autobox> module provides the ability for calling 'methods' |
116 | on normal Perl values like Scalars, Arrays, Hashes and Code |
117 | references. This gives the illusion that Perl's types are first-class |
118 | objects. However, this is only an illusion, albeit a very nice one. |
119 | I created this module because L<autobox> itself does not actually |
120 | provide an implementation for the Perl types but instead only provides |
121 | the 'hooks' for others to add implementation too. |
122 | |
123 | =head2 Is this for real? or just play? |
124 | |
125 | My intent is to try and make this module as production worthy as |
126 | possible. This may or may not be possible, depending on how well |
127 | L<autobox> works out. At this point, I have high hopes for things |
128 | but only time (and more tests and code) will tell. |
129 | |
244bd352 |
130 | =head1 METHODS |
131 | |
132 | =over 4 |
133 | |
134 | =item B<mixin_additional_role ($type, $role)> |
135 | |
136 | This will mixin an additonal C<$role> into a certain C<$type>. The |
137 | types can be SCALAR, ARRAY, HASH or CODE. |
138 | |
139 | This can be used to add additional methods to the types, see the |
140 | F<examples/units/> directory for some examples. |
141 | |
142 | =back |
1972aa1b |
143 | |
144 | =head1 TODO |
145 | |
146 | =over 4 |
147 | |
148 | =item More docs |
149 | |
150 | =item More tests |
151 | |
152 | =back |
31d40d73 |
153 | |
5f654d8e |
154 | =head1 BUGS |
155 | |
156 | All complex software has bugs lurking in it, and this module is no |
157 | exception. If you find a bug please either email me, or add the bug |
158 | to cpan-RT. |
159 | |
160 | =head1 AUTHOR |
161 | |
162 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
163 | |
b3cb7038 |
164 | B<with contributions from:> |
165 | |
166 | Anders (Debolaz) Nor Berle |
167 | |
168 | Matt (mst) Trout |
169 | |
170 | renormalist |
171 | |
5f654d8e |
172 | =head1 COPYRIGHT AND LICENSE |
173 | |
b3cb7038 |
174 | Copyright 2006-2007 by Infinity Interactive, Inc. |
5f654d8e |
175 | |
176 | L<http://www.iinteractive.com> |
177 | |
178 | This library is free software; you can redistribute it and/or modify |
179 | it under the same terms as Perl itself. |
180 | |
5f654d8e |
181 | =cut |