File Coverage

blib/lib/MooseX/ArrayRef.pm
Criterion Covered Total %
statement 6 8 75.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 9 11 81.8


line stmt bran cond sub pod time code
1             package MooseX::ArrayRef;
2              
3 4     4   106169 use 5.008;
  4         15  
  4         217  
4              
5             BEGIN {
6 4     4   18 $MooseX::ArrayRef::AUTHORITY = 'cpan:TOBYINK';
7 4         70 $MooseX::ArrayRef::VERSION = '0.005';
8             }
9              
10 4     4   6008 use Moose ();
  0            
  0            
11             use Moose::Exporter;
12             use Moose::Util::MetaRole;
13             use MooseX::ArrayRef::Meta::Instance ();
14             use MooseX::ArrayRef::Meta::Class ();
15              
16             Moose::Exporter->setup_import_methods(
17             also => [qw( Moose )],
18             );
19              
20             sub init_meta
21             {
22             shift;
23             my %p = @_;
24             Moose->init_meta(%p);
25             Moose::Util::MetaRole::apply_metaroles(
26             for => $p{for_class},
27             class_metaroles => {
28             instance => [qw( MooseX::ArrayRef::Meta::Instance )],
29             class => [qw( MooseX::ArrayRef::Meta::Class )],
30             },
31             );
32             }
33              
34             [qw( Yeah baby yeah )]
35              
36             __END__
37              
38             =head1 NAME
39              
40             MooseX::ArrayRef - blessed arrayrefs with Moose
41              
42             =head1 SYNOPSIS
43              
44             {
45             package Local::Person;
46             use Moose;
47             has name => (
48             is => 'ro',
49             isa => 'Str',
50             );
51             __PACKAGE__->meta->make_immutable;
52             }
53            
54             {
55             package Local::Marriage;
56             use MooseX::ArrayRef;
57             has husband => (
58             is => 'ro',
59             isa => 'Local::Person',
60             );
61             has wife => (
62             is => 'ro',
63             isa => 'Local::Person',
64             );
65             __PACKAGE__->meta->make_immutable;
66             }
67            
68             my $marriage = Local::Marriage->new(
69             wife => Local::Person->new(name => 'Alex'),
70             husband => Local::Person->new(name => 'Sam'),
71             );
72            
73             use Data::Dumper;
74             use Scalar::Util qw(reftype);
75             print reftype($marriage), "\n"; # 'ARRAY'
76             print Dumper($marriage);
77              
78              
79             =head1 DESCRIPTION
80              
81             Objects implemented with arrayrefs rather than hashrefs are often faster than
82             those implemented with hashrefs. Moose's default object implementation is
83             hashref based. Can we go faster?
84              
85             Simply C<< use MooseX::ArrayRef >> instead of C<< use Moose >>, but note the
86             limitations in the section below.
87              
88             The current implementation is mostly a proof of concept, but it does mostly
89             seem to work.
90              
91             =begin private
92              
93             =item init_meta
94              
95             =end private
96              
97             =head1 BUGS AND LIMITATIONS
98              
99             =head2 Limitations on Speed
100              
101             The accessors for mutable classes not significantly faster than Moose's
102             traditional hashref-based objects. For immutable classes, the speed up
103             is bigger
104              
105             Rate HashRef_M ArrayRef_M HashRef_I ArrayRef_I
106             HashRef_M 1016/s -- -1% -48% -55%
107             ArrayRef_M 1031/s 1% -- -47% -54%
108             HashRef_I 1953/s 92% 89% -- -13%
109             ArrayRef_I 2257/s 122% 119% 16% --
110              
111             =head2 Limitations on Mutability
112              
113             Things will probably break if you try to modify classes, add roles, etc "on
114             the fly". Make your classes immutable before instantiating even a single
115             object.
116              
117             =head2 Limitations on Inheritance
118              
119             Inheritance isn't easy to implement with arrayrefs. The current implementation
120             suffers from the following limitations:
121              
122             =over
123              
124             =item * Single inheritance only.
125              
126             You cannot extend multiple parent classes.
127              
128             =item * Inherit from other MooseX::ArrayRef classes only.
129              
130             A MooseX::ArrayRef class cannot extend a non-MooseX::ArrayRef class.
131             Even non-Moose classes which are implemented using arrayrefs. (Of
132             course, all Moose classes inherit from L<Moose::Object> too, which
133             is just fine.)
134              
135             =back
136              
137             Note that delegation (via Moose's C<handles>) is often a good alternative
138             to inheritance.
139              
140             =head2 Issue Tracker
141              
142             Please report any bugs to
143             L<http://rt.cpan.org/Dist/Display.html?Queue=MooseX-ArrayRef>.
144              
145             =head1 SEE ALSO
146              
147             L<Moose>,
148             L<MooseX::GlobRef>,
149             L<MooseX::InsideOut>.
150              
151             =head1 AUTHOR
152              
153             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
154              
155             =head1 COPYRIGHT AND LICENCE
156              
157             This software is copyright (c) 2012 by Toby Inkster.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =head1 DISCLAIMER OF WARRANTIES
163              
164             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
165             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
166             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
167