File Coverage

blib/lib/Moose/Object.pm
Criterion Covered Total %
statement 77 84 91.6
branch 20 24 83.3
condition 13 15 86.6
subroutine 20 22 90.9
pod 5 7 71.4
total 135 152 88.8


line stmt bran cond sub pod time code
1             package Moose::Object;
2             our $VERSION = '2.2206';
3              
4 379     379   5501 use strict;
  379         968  
  379         12166  
5 379     379   2186 use warnings;
  379         1142  
  379         9878  
6              
7 379     379   2513 use Carp ();
  379         1056  
  379         6578  
8 379     379   2807 use Devel::GlobalDestruction ();
  379         2830  
  379         6785  
9 379     379   2806 use MRO::Compat ();
  379         2675  
  379         7402  
10 379     379   2491 use Scalar::Util ();
  379         1087  
  379         7754  
11 379     379   2982 use Try::Tiny ();
  379         3096  
  379         7146  
12              
13 379     379   2835 use Moose::Util ();
  379         1077  
  379         15753  
14              
15 379     379   272142 use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class';
  379         5969  
  379         2694  
16 379     379   38626 use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class';
  379         963  
  379         1598  
17              
18             sub new {
19 3438     3438 1 988798 my $class = shift;
20 3438   66     14921 my $real_class = Scalar::Util::blessed($class) || $class;
21              
22 3438         9781 my $params = $real_class->BUILDARGS(@_);
23              
24 3435         12608 return Class::MOP::Class->initialize($real_class)->new_object($params);
25             }
26              
27             sub BUILDARGS {
28 3428     3428 1 5512 my $class = shift;
29 3428 100       11237 if ( scalar @_ == 1 ) {
    50          
30 14 100 100     150 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
31 3         12 Moose::Util::throw_exception( "SingleParamsToNewMustBeHashRef" );
32             }
33 11         26 return { %{ $_[0] } };
  11         61  
34             }
35             elsif ( @_ % 2 ) {
36 0         0 Carp::carp(
37             "The new() method for $class expects a hash reference or a key/value list."
38             . " You passed an odd number of arguments" );
39 0         0 return { @_, undef };
40             }
41             else {
42 3414         8953 return { @_ };
43             }
44             }
45              
46             sub BUILDALL {
47             # NOTE: we ask Perl if we even
48             # need to do this first, to avoid
49             # extra meta level calls
50 2451 100   2451 0 9958 return unless $_[0]->can('BUILD');
51 30         78 my ($self, $params) = @_;
52 30 100       91 return if $params->{__no_BUILD__};
53 28         115 foreach my $method (reverse Class::MOP::class_of($self)->find_all_methods_by_name('BUILD')) {
54 28         153 $method->{code}->execute($self, $params);
55             }
56             }
57              
58             sub DEMOLISHALL {
59 3419     3419 0 21808 my $self = shift;
60 3419         6444 my ($in_global_destruction) = @_;
61              
62             # NOTE: we ask Perl if we even
63             # need to do this first, to avoid
64             # extra meta level calls
65 3419 100       15266 return unless $self->can('DEMOLISH');
66              
67 56         111 my @isa;
68 56 100       204 if ( my $meta = Class::MOP::class_of($self ) ) {
69 55         181 @isa = $meta->linearized_isa;
70             } else {
71             # We cannot count on being able to retrieve a previously made
72             # metaclass, _or_ being able to make a new one during global
73             # destruction. However, we should still be able to use mro at
74             # that time (at least tests suggest so ;)
75 1         4 my $class_name = ref $self;
76 1         2 @isa = @{ mro::get_linear_isa($class_name) }
  1         5  
77             }
78              
79 56         151 foreach my $class (@isa) {
80 379     379   3553 no strict 'refs';
  379         1242  
  379         98397  
81 119         1452 my $demolish = *{"${class}::DEMOLISH"}{CODE};
  119         467  
82 119 100       516 $self->$demolish($in_global_destruction)
83             if defined $demolish;
84             }
85             }
86              
87             sub DESTROY {
88 3419     3419   447185 my $self = shift;
89              
90 3419         10379 local $?;
91              
92             # < doy> if the destructor is being called because an exception is thrown, then $@ will be set
93             # < doy> but if DEMOLISH does an eval which succeeds, that will clear $@
94             # < doy> which is broken
95             # < doy> try::tiny implicitly localizes $@ in the try block, which fixes that
96             Try::Tiny::try {
97 3419     3419   200870 $self->DEMOLISHALL(Devel::GlobalDestruction::in_global_destruction);
98             }
99             Try::Tiny::catch {
100 0     0   0 die $_;
101 3419         19218 };
102              
103 3419         70415 return;
104             }
105              
106             # support for UNIVERSAL::DOES ...
107             BEGIN {
108 379 50   379   4499 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
109 379   100 57 1 103138 eval 'sub DOES {
  57         4632  
  57         703  
110             my ( $self, $class_or_role_name ) = @_;
111             return $self->'.$does.'($class_or_role_name)
112             || $self->does($class_or_role_name);
113             }';
114             }
115              
116             # new does() methods will be created
117             # as appropriate see Moose::Meta::Role
118             sub does {
119 540     540 1 31497 my ($self, $role_name) = @_;
120 540   66     2797 my $class = Scalar::Util::blessed($self) || $self;
121 540         2389 my $meta = Class::MOP::Class->initialize($class);
122 540 100       2215 (defined $role_name)
123             || Moose::Util::throw_exception( DoesRequiresRoleName => class_name => $meta->name );
124 537 100 100     4077 return 1 if $meta->can('does_role') && $meta->does_role($role_name);
125 325         1777 return 0;
126             }
127              
128             sub dump {
129 0     0 1   my $self = shift;
130 0           require Data::Dumper;
131 0 0         local $Data::Dumper::Maxdepth = shift if @_;
132 0           Data::Dumper::Dumper $self;
133             }
134              
135             1;
136              
137             # ABSTRACT: The base object for Moose
138              
139             __END__
140              
141             =pod
142              
143             =encoding UTF-8
144              
145             =head1 NAME
146              
147             Moose::Object - The base object for Moose
148              
149             =head1 VERSION
150              
151             version 2.2206
152              
153             =head1 DESCRIPTION
154              
155             This class is the default base class for all Moose-using classes. When
156             you C<use Moose> in this class, your class will inherit from this
157             class.
158              
159             It provides a default constructor and destructor, which run all of the
160             C<BUILD> and C<DEMOLISH> methods in the inheritance hierarchy,
161             respectively.
162              
163             You don't actually I<need> to inherit from this in order to use Moose,
164             but it makes it easier to take advantage of all of Moose's features.
165              
166             =head1 METHODS
167              
168             =head2 Moose::Object->new(%params|$params)
169              
170             This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
171             instance of the appropriate class. Once the instance is created, it
172             calls C<< $instance->BUILD($params) >> for each C<BUILD> method in the
173             inheritance hierarchy.
174              
175             =head2 Moose::Object->BUILDARGS(%params|$params)
176              
177             The default implementation of this method accepts a hash or hash
178             reference of named parameters. If it receives a single argument that
179             I<isn't> a hash reference it throws an error.
180              
181             You can override this method in your class to handle other types of
182             options passed to the constructor.
183              
184             This method should always return a hash reference of named options.
185              
186             =head2 $object->does($role_name)
187              
188             This returns true if the object does the given role.
189              
190             =head2 $object->DOES($class_or_role_name)
191              
192             This is a Moose role-aware implementation of L<UNIVERSAL/DOES>.
193              
194             This is effectively the same as writing:
195              
196             $object->does($name) || $object->isa($name)
197              
198             This method will work with Perl 5.8, which did not implement
199             C<UNIVERSAL::DOES>.
200              
201             =head2 $object->dump($maxdepth)
202              
203             =for stopwords ing
204              
205             This is a handy utility for L<Data::Dumper>ing an object. By default,
206             there is no maximum depth.
207              
208             =head2 $object->DESTROY
209              
210             A default destructor is provided, which calls
211             C<< $instance->DEMOLISH($in_global_destruction) >> for each C<DEMOLISH>
212             method in the inheritance hierarchy.
213              
214             =head1 BUGS
215              
216             See L<Moose/BUGS> for details on reporting bugs.
217              
218             =head1 AUTHORS
219              
220             =over 4
221              
222             =item *
223              
224             Stevan Little <stevan@cpan.org>
225              
226             =item *
227              
228             Dave Rolsky <autarch@urth.org>
229              
230             =item *
231              
232             Jesse Luehrs <doy@cpan.org>
233              
234             =item *
235              
236             Shawn M Moore <sartak@cpan.org>
237              
238             =item *
239              
240             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
241              
242             =item *
243              
244             Karen Etheridge <ether@cpan.org>
245              
246             =item *
247              
248             Florian Ragwitz <rafl@debian.org>
249              
250             =item *
251              
252             Hans Dieter Pearcey <hdp@cpan.org>
253              
254             =item *
255              
256             Chris Prather <chris@prather.org>
257              
258             =item *
259              
260             Matt S Trout <mstrout@cpan.org>
261              
262             =back
263              
264             =head1 COPYRIGHT AND LICENSE
265              
266             This software is copyright (c) 2006 by Infinity Interactive, Inc.
267              
268             This is free software; you can redistribute it and/or modify it under
269             the same terms as the Perl 5 programming language system itself.
270              
271             =cut