Remove all trailing whitespace
[gitmo/Moose.git] / lib / Class / MOP / Object.pm
CommitLineData
38bf2a25 1
2package Class::MOP::Object;
3
4use strict;
5use warnings;
6
7use Carp qw(confess);
8use Scalar::Util 'blessed';
9
38bf2a25 10# introspection
11
064a13a3 12sub meta {
38bf2a25 13 require Class::MOP::Class;
14 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
15}
16
17sub _new {
18 Class::MOP::class_of(shift)->new_object(@_);
19}
20
21# RANT:
064a13a3 22# Cmon, how many times have you written
38bf2a25 23# the following code while debugging:
064a13a3 24#
25# use Data::Dumper;
38bf2a25 26# warn Dumper $obj;
27#
064a13a3 28# It can get seriously annoying, so why
38bf2a25 29# not just do this ...
064a13a3 30sub dump {
38bf2a25 31 my $self = shift;
32 require Data::Dumper;
33 local $Data::Dumper::Maxdepth = shift || 1;
34 Data::Dumper::Dumper $self;
35}
36
37sub _real_ref_name {
38 my $self = shift;
39 return blessed($self);
40}
41
42sub _is_compatible_with {
43 my $self = shift;
44 my ($other_name) = @_;
45
46 return $self->isa($other_name);
47}
48
49sub _can_be_made_compatible_with {
50 my $self = shift;
51 return !$self->_is_compatible_with(@_)
52 && defined($self->_get_compatible_metaclass(@_));
53}
54
55sub _make_compatible_with {
56 my $self = shift;
57 my ($other_name) = @_;
58
59 my $new_metaclass = $self->_get_compatible_metaclass($other_name);
60
61 confess "Can't make $self compatible with metaclass $other_name"
62 unless defined $new_metaclass;
63
64 # can't use rebless_instance here, because it might not be an actual
65 # subclass in the case of, e.g. moose role reconciliation
66 $new_metaclass->meta->_force_rebless_instance($self)
67 if blessed($self) ne $new_metaclass;
68
69 return $self;
70}
71
72sub _get_compatible_metaclass {
73 my $self = shift;
74 my ($other_name) = @_;
75
76 return $self->_get_compatible_metaclass_by_subclassing($other_name);
77}
78
79sub _get_compatible_metaclass_by_subclassing {
80 my $self = shift;
81 my ($other_name) = @_;
82 my $meta_name = blessed($self) ? $self->_real_ref_name : $self;
83
84 if ($meta_name->isa($other_name)) {
85 return $meta_name;
86 }
87 elsif ($other_name->isa($meta_name)) {
88 return $other_name;
89 }
90
91 return;
92}
93
941;
95
064a13a3 96# ABSTRACT: Base class for metaclasses
97
38bf2a25 98__END__
99
100=pod
101
38bf2a25 102=head1 DESCRIPTION
103
104This class is a very minimal base class for metaclasses.
105
106=head1 METHODS
107
108This class provides a few methods which are useful in all metaclasses.
109
110=over 4
111
112=item B<< Class::MOP::???->meta >>
113
114This returns a L<Class::MOP::Class> object.
115
116=item B<< $metaobject->dump($max_depth) >>
117
118This method uses L<Data::Dumper> to dump the object. You can pass an
119optional maximum depth, which will set C<$Data::Dumper::Maxdepth>. The
120default maximum depth is 1.
121
122=back
123
124=cut