File: //usr/local/lib64/perl5/Cpanel/Class/Object.pm
package Cpanel::Class::Object;
use strict;
use warnings;
use Carp 'confess';
use Scalar::Util 'blessed', 'reftype';
our $VERSION = '1.0.5';
=pod
=head1 new
Constructer
It will call a BUILD method on every class in the inheritance tree starting at the parent. BUILD is also passed a hash_ref with the arguments passed to new.
=cut
sub new {
my $class = shift;
my %params;
if ( scalar @_ == 1 ) {
if ( defined $_[0] ) {
( ref( $_[0] ) eq 'HASH' )
|| confess "Single parameters to new() must be a HASH ref";
%params = %{ $_[0] };
}
}
else {
%params = @_;
}
my $self = $class->meta->new_object( \%params );
$self->BUILDALL( \%params );
return $self;
}
sub BUILDALL {
my ( $self, $params ) = @_;
foreach my $build ( $self->meta->find_all_methods_by_name('BUILD') ) {
$build->{code}->( $self, $params );
}
}
sub DEMOLISHALL {
return unless $_[0]->can('DEMOLISH');
my $self = shift;
foreach my $method ( $self->meta->find_all_methods_by_name('DEMOLISH') ) {
$method->{code}->($self);
}
}
sub DESTROY { goto &DEMOLISHALL }
sub meta {
my ($self) = @_;
my $class = ref $self || $self;
return Cpanel::Class::Meta::Class->initialize($class);
}
sub AUTOLOAD {
my ($self) = @_;
my ( $package_name, $method_name ) = our $AUTOLOAD =~ m/ (.*) :: (.*) /xms;
return if $method_name eq 'DESTROY';
foreach my $automethod ( $self->meta->find_all_methods_by_name('AUTOMETHOD') ) {
local $CALLER::_ = $_;
local $_ = $method_name;
if ( my $method_impl = $automethod->{code}->( $self, @_[ 1 .. $#_ ] ) ) {
$self->meta->add_method( $method_name => $method_impl );
goto &$method_impl;
}
}
my $type = ref $self ? 'object' : 'class';
confess qq{Can't locate $type method "$method_name" via package "$package_name"};
}
sub dump {
my $self = shift;
require Data::Dumper;
$Data::Dumper::Maxdepth = shift || 4;
Data::Dumper::Dumper $self;
}
1;