use Scalar::Util 'weaken', 'reftype', 'blessed';
use Try::Tiny;
+use Class::MOP::HasAttributes;
+use Class::MOP::HasMethods;
use Class::MOP::Class;
use Class::MOP::Attribute;
use Class::MOP::Method;
*check_package_cache_flag = \&mro::get_pkg_gen;
}
-our $VERSION = '0.94';
+our $VERSION = '0.95';
our $XS_VERSION = $VERSION;
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
sub load_first_existing_class {
my @classes = @_
- or return;
+ or return;
foreach my $class (@classes) {
unless ( _is_valid_class_name($class) ) {
my $found;
my %exceptions;
- for my $class (@classes) {
- my ($fail, $e) = _try_load_one_class($class);
- if ($fail) {
- my $pmfile = _class_to_pmfile($class);
- $exceptions{$class} = $e;
- last if $e !~ /^Can't locate \Q$pmfile\E in \@INC/;
- }
- else {
- $found = $class;
- last;
- }
- }
+ for my $class (@classes) {
+ my $file = _class_to_pmfile($class);
- return $found if $found;
+ return $class if is_class_loaded($class);;
- confess join(
- "\n",
- map {
- sprintf(
- "Could not load class (%s) because : %s", $_,
- $exceptions{$_}
- )
+ return $class if try {
+ local $SIG{__DIE__};
+ require $file;
+ return 1;
+ }
+ catch {
+ unless (/^Can't locate \Q$file\E in \@INC/) {
+ confess "Couldn't load class ($class) because: $_";
}
- grep {
- exists $exceptions{$_}
- } @classes
- );
-}
-sub _try_load_one_class {
- my $class = shift;
-
- return if is_class_loaded($class);
-
- my $file = _class_to_pmfile($class);
-
- my ($failed, $error);
- try {
- local $SIG{__DIE__};
- require($file);
+ return;
+ };
}
- catch {
- $failed = 1;
- $error = $_;
- };
- return $failed, $error;
+ if ( @classes > 1 ) {
+ confess "Can't locate any of @classes in \@INC (\@INC contains: @INC).";
+ } else {
+ confess "Can't locate " . _class_to_pmfile($classes[0]) . " in \@INC (\@INC contains: @INC).";
+ }
}
sub load_class {
# inherit them using _construct_instance
## --------------------------------------------------------
-## Class::MOP::Package
+## Class::MOP::HasMethods
-Class::MOP::Package->meta->add_attribute(
- Class::MOP::Attribute->new('package' => (
+Class::MOP::HasMethods->meta->add_attribute(
+ Class::MOP::Attribute->new('_methods' => (
reader => {
- # NOTE: we need to do this in order
- # for the instance meta-object to
- # not fall into meta-circular death
- #
+ # NOTE:
# we just alias the original method
# rather than re-produce it here
- 'name' => \&Class::MOP::Package::name
+ '_full_method_map' => \&Class::MOP::HasMethods::_full_method_map
},
+ default => sub { {} }
))
);
-Class::MOP::Package->meta->add_attribute(
- Class::MOP::Attribute->new('namespace' => (
- reader => {
+Class::MOP::HasMethods->meta->add_attribute(
+ Class::MOP::Attribute->new('method_metaclass' => (
+ reader => {
# NOTE:
# we just alias the original method
# rather than re-produce it here
- 'namespace' => \&Class::MOP::Package::namespace
+ 'method_metaclass' => \&Class::MOP::HasMethods::method_metaclass
},
- init_arg => undef,
- default => sub { \undef }
+ default => 'Class::MOP::Method',
))
);
-Class::MOP::Package->meta->add_attribute(
- Class::MOP::Attribute->new('_methods' => (
+Class::MOP::HasMethods->meta->add_attribute(
+ Class::MOP::Attribute->new('wrapped_method_metaclass' => (
reader => {
# NOTE:
# we just alias the original method
# rather than re-produce it here
- '_full_method_map' => \&Class::MOP::Package::_full_method_map
+ 'wrapped_method_metaclass' => \&Class::MOP::HasMethods::wrapped_method_metaclass
},
- default => sub { {} }
+ default => 'Class::MOP::Method::Wrapped',
))
);
-Class::MOP::Package->meta->add_attribute(
- Class::MOP::Attribute->new('method_metaclass' => (
+## --------------------------------------------------------
+## Class::MOP::HasMethods
+
+Class::MOP::HasAttributes->meta->add_attribute(
+ Class::MOP::Attribute->new('attributes' => (
+ reader => {
+ # NOTE: we need to do this in order
+ # for the instance meta-object to
+ # not fall into meta-circular death
+ #
+ # we just alias the original method
+ # rather than re-produce it here
+ '_attribute_map' => \&Class::MOP::HasAttributes::_attribute_map
+ },
+ default => sub { {} }
+ ))
+);
+
+Class::MOP::HasAttributes->meta->add_attribute(
+ Class::MOP::Attribute->new('attribute_metaclass' => (
reader => {
# NOTE:
# we just alias the original method
# rather than re-produce it here
- 'method_metaclass' => \&Class::MOP::Package::method_metaclass
+ 'attribute_metaclass' => \&Class::MOP::HasAttributes::attribute_metaclass
},
- default => 'Class::MOP::Method',
+ default => 'Class::MOP::Attribute',
))
);
+## --------------------------------------------------------
+## Class::MOP::Package
+
Class::MOP::Package->meta->add_attribute(
- Class::MOP::Attribute->new('wrapped_method_metaclass' => (
+ Class::MOP::Attribute->new('package' => (
reader => {
+ # NOTE: we need to do this in order
+ # for the instance meta-object to
+ # not fall into meta-circular death
+ #
+ # we just alias the original method
+ # rather than re-produce it here
+ 'name' => \&Class::MOP::Package::name
+ },
+ ))
+);
+
+Class::MOP::Package->meta->add_attribute(
+ Class::MOP::Attribute->new('namespace' => (
+ reader => {
# NOTE:
# we just alias the original method
# rather than re-produce it here
- 'wrapped_method_metaclass' => \&Class::MOP::Package::wrapped_method_metaclass
+ 'namespace' => \&Class::MOP::Package::namespace
},
- default => 'Class::MOP::Method::Wrapped',
+ init_arg => undef,
+ default => sub { \undef }
))
);
## Class::MOP::Class
Class::MOP::Class->meta->add_attribute(
- Class::MOP::Attribute->new('attributes' => (
- reader => {
- # NOTE: we need to do this in order
- # for the instance meta-object to
- # not fall into meta-circular death
- #
- # we just alias the original method
- # rather than re-produce it here
- 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
- },
- default => sub { {} }
- ))
-);
-
-Class::MOP::Class->meta->add_attribute(
Class::MOP::Attribute->new('superclasses' => (
accessor => {
# NOTE:
);
Class::MOP::Class->meta->add_attribute(
- Class::MOP::Attribute->new('attribute_metaclass' => (
- reader => {
- # NOTE:
- # we just alias the original method
- # rather than re-produce it here
- 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
- },
- default => 'Class::MOP::Attribute',
- ))
-);
-
-Class::MOP::Class->meta->add_attribute(
Class::MOP::Attribute->new('instance_metaclass' => (
reader => {
# NOTE: we need to do this in order
Class::MOP::Method::Wrapped
/;
+$_->meta->make_immutable(
+ inline_constructor => 0,
+ constructor_name => undef,
+ inline_accessors => 0,
+) for qw/
+ Class::MOP::HasAttributes
+ Class::MOP::HasMethods
+/;
+
1;
__END__
If the module cannot be loaded, an exception is thrown.
-For historical reasons, this function returns explicitly returns a true value.
+For historical reasons, this function explicitly returns a true value.
=item B<Class::MOP::is_class_loaded($class_name)>