From: Dave Rolsky Date: Fri, 20 Feb 2009 18:06:53 +0000 (+0000) Subject: We need to make sure Moose is loaded before using X-Git-Tag: 0.71_01~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=70ea916185583ff7738ef8b8fe6aee48cdce9b23;p=gitmo%2FMoose.git We need to make sure Moose is loaded before using Moose->throw_error. Normally Moose is loaded, but in cases where it's not we end up losing the real error, which is very annoying. --- diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index 980bee5..036a744 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -262,6 +262,7 @@ sub _make_import_sub { _apply_meta_traits( $CALLER, $traits ); } elsif ( @{$traits} ) { + require Moose; Moose->throw_error( "Cannot provide traits when $class does not have an init_meta() method" ); diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm index 8a43ef1..4679dc9 100644 --- a/lib/Moose/Meta/Method/Constructor.pm +++ b/lib/Moose/Meta/Method/Constructor.pm @@ -117,7 +117,7 @@ sub associated_metaclass { (shift)->{'associated_metaclass'} } # this was changed in 0.41, but broke MooseX::Singleton, so try to catch # any other code using the original broken spelling -sub intialize_body { Moose->throw_error("Please correct the spelling of 'intialize_body' to 'initialize_body'") } +sub intialize_body { $_[0]->throw_error("Please correct the spelling of 'intialize_body' to 'initialize_body'") } sub initialize_body { my $self = shift; diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 4260f92..30e2273 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -122,8 +122,10 @@ foreach my $action ( sub add_attribute { my $self = shift; my $name = shift; - (defined $name && $name) - || Moose->throw_error("You must provide a name for the attribute"); + unless ( defined $name && $name ) { + require Moose; + Moose->throw_error("You must provide a name for the attribute"); + } my $attr_desc; if (scalar @_ == 1 && ref($_[0]) eq 'HASH') { $attr_desc = $_[0]; diff --git a/lib/Moose/Meta/Role/Application/RoleSummation.pm b/lib/Moose/Meta/Role/Application/RoleSummation.pm index 3ac259c..66c2381 100644 --- a/lib/Moose/Meta/Role/Application/RoleSummation.pm +++ b/lib/Moose/Meta/Role/Application/RoleSummation.pm @@ -79,6 +79,8 @@ sub check_role_exclusions { next unless $role->does_role($excluded); my @excluding = @{ $excluded_roles{$excluded} }; + + require Moose; Moose->throw_error(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded); } } @@ -125,9 +127,11 @@ sub apply_attributes { my %seen; foreach my $attr (@all_attributes) { if (exists $seen{$attr->{name}}) { - Moose->throw_error("We have encountered an attribute conflict with '" . $attr->{name} . "' " - . "during composition. This is fatal error and cannot be disambiguated.") - if $seen{$attr->{name}} != $attr->{attr}; + if ( $seen{$attr->{name}} != $attr->{attr} ) { + require Moose; + Moose->throw_error("We have encountered an attribute conflict with '" . $attr->{name} . "' " + . "during composition. This is fatal error and cannot be disambiguated.") + } } $seen{$attr->{name}} = $attr->{attr}; } @@ -195,15 +199,19 @@ sub apply_override_method_modifiers { my %seen; foreach my $override (@all_overrides) { - Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " . - "during composition (A local method of the same name as been found). This " . - "is fatal error." ) - if $c->has_method($override->{name}); + if ( $c->has_method($override->{name}) ){ + require Moose; + Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " . + "during composition (A local method of the same name as been found). This " . + "is fatal error." ) + } if (exists $seen{$override->{name}}) { - Moose->throw_error( "We have encountered an 'override' method conflict during " . - "composition (Two 'override' methods of the same name encountered). " . - "This is fatal error.") - if $seen{$override->{name}} != $override->{method}; + if ( $seen{$override->{name}} != $override->{method} ) { + require Moose; + Moose->throw_error( "We have encountered an 'override' method conflict during " . + "composition (Two 'override' methods of the same name encountered). " . + "This is fatal error.") + } } $seen{$override->{name}} = $override->{method}; } diff --git a/lib/Moose/Meta/Role/Application/ToRole.pm b/lib/Moose/Meta/Role/Application/ToRole.pm index 6c6cb84..c7b06d7 100644 --- a/lib/Moose/Meta/Role/Application/ToRole.pm +++ b/lib/Moose/Meta/Role/Application/ToRole.pm @@ -20,11 +20,15 @@ sub apply { sub check_role_exclusions { my ($self, $role1, $role2) = @_; - Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'") - if $role2->excludes_role($role1->name); + if ( $role2->excludes_role($role1->name) ) { + require Moose; + Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'"); + } foreach my $excluded_role_name ($role1->get_excluded_roles_list) { - Moose->throw_error("The class " . $role2->name . " does the excluded role '$excluded_role_name'") - if $role2->does_role($excluded_role_name); + if ( $role2->does_role($excluded_role_name) ) { + require Moose; + Moose->throw_error("The class " . $role2->name . " does the excluded role '$excluded_role_name'"); + } $role2->add_excluded_roles($excluded_role_name); } } @@ -51,6 +55,8 @@ sub apply_attributes { if ($role2->has_attribute($attribute_name) && # make sure we haven't seen this one already too $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) { + + require Moose; Moose->throw_error("Role '" . $role1->name . "' has encountered an attribute conflict " . "during composition. This is fatal error and cannot be disambiguated."); } @@ -73,6 +79,8 @@ sub apply_methods { if ($role2->has_method($aliased_method_name) && # and if they are not the same thing ... $role2->get_method($aliased_method_name)->body != $role1->get_method($method_name)->body) { + + require Moose; Moose->throw_error("Cannot create a method alias if a local method of the same name exists"); } @@ -120,6 +128,7 @@ sub apply_override_method_modifiers { # we have a conflict here, because you cannot # combine an overridden method with a locally # defined one + require Moose; Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " . "during composition (A local method of the same name as been found). This " . "is fatal error."); @@ -130,6 +139,8 @@ sub apply_override_method_modifiers { # we are composing into if ($role2->has_override_method_modifier($method_name) && $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) { + + require Moose; Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " . "during composition (Two 'override' methods of the same name encountered). " . "This is fatal error."); diff --git a/lib/Moose/Meta/Role/Composite.pm b/lib/Moose/Meta/Role/Composite.pm index 066329d..1e7e6dd 100644 --- a/lib/Moose/Meta/Role/Composite.pm +++ b/lib/Moose/Meta/Role/Composite.pm @@ -33,9 +33,12 @@ __PACKAGE__->meta->add_attribute('methods' => ( sub new { my ($class, %params) = @_; # the roles param is required ... - ($_->isa('Moose::Meta::Role')) - || Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_") - foreach @{$params{roles}}; + foreach ( @{$params{roles}} ) { + unless ( $_->isa('Moose::Meta::Role') ) { + require Moose; + Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_"); + } + } # and the name is created from the # roles if one has not been provided $params{name} ||= (join "|" => map { $_->name } @{$params{roles}}); @@ -48,8 +51,10 @@ sub new { # add the symbol. sub add_method { my ($self, $method_name, $method) = @_; - (defined $method_name && $method_name) - || Moose->throw_error("You must define a method name"); + + unless ( defined $method_name && $method_name ) { + Moose->throw_error("You must define a method name"); + } my $body; if (blessed($method)) { diff --git a/lib/Moose/Meta/TypeCoercion.pm b/lib/Moose/Meta/TypeCoercion.pm index 646044d..0339be8 100644 --- a/lib/Moose/Meta/TypeCoercion.pm +++ b/lib/Moose/Meta/TypeCoercion.pm @@ -43,8 +43,12 @@ sub compile_type_coercion { while (@coercion_map) { my ($constraint_name, $action) = splice(@coercion_map, 0, 2); my $type_constraint = ref $constraint_name ? $constraint_name : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name); - (defined $type_constraint) - || Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from"); + + unless ( defined $type_constraint ) { + require Moose; + Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from"); + } + push @coercions => [ $type_constraint->_compiled_type_constraint, $action @@ -77,10 +81,12 @@ sub add_type_coercions { while (@new_coercion_map) { my ($constraint_name, $action) = splice(@new_coercion_map, 0, 2); - - Moose->throw_error("A coercion action already exists for '$constraint_name'") - if exists $has_coercion{$constraint_name}; - + + if ( exists $has_coercion{$constraint_name} ) { + require Moose; + Moose->throw_error("A coercion action already exists for '$constraint_name'") + } + push @{$coercion_map} => ($constraint_name, $action); } diff --git a/lib/Moose/Meta/TypeCoercion/Union.pm b/lib/Moose/Meta/TypeCoercion/Union.pm index c417ae6..4cb86e2 100644 --- a/lib/Moose/Meta/TypeCoercion/Union.pm +++ b/lib/Moose/Meta/TypeCoercion/Union.pm @@ -43,6 +43,7 @@ sub compile_type_coercion { sub has_coercion_for_type { 0 } sub add_type_coercions { + require Moose; Moose->throw_error("Cannot add additional type coercions to Union types"); } diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index 5ba7126..16c8d6b 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -36,6 +36,7 @@ __PACKAGE__->meta->add_attribute('coercion' => ( accessor => 'coercion', predicate => 'has_coercion' )); + __PACKAGE__->meta->add_attribute('hand_optimized_type_constraint' => ( init_arg => 'optimized', accessor => 'hand_optimized_type_constraint', @@ -62,7 +63,7 @@ sub new { my ($first, @rest) = @_; my %args = ref $first ? %$first : $first ? ($first, @rest) : (); $args{name} = $args{name} ? "$args{name}" : "__ANON__"; - + my $self = $class->_new(%args); $self->compile_type_constraint() unless $self->_has_compiled_type_constraint; @@ -71,7 +72,18 @@ sub new { -sub coerce { ((shift)->coercion || Moose->throw_error("Cannot coerce without a type coercion"))->coerce(@_) } +sub coerce { + my $self = shift; + + my $coercion = $self->coercion; + + unless ($coercion) { + require Moose; + Moose->throw_error("Cannot coerce without a type coercion"); + } + + return $coercion->coerce(@_); +} sub check { my ($self, @args) = @_; @@ -165,10 +177,12 @@ sub _actually_compile_type_constraint { if $self->has_hand_optimized_type_constraint; my $check = $self->constraint; - (defined $check) - || Moose->throw_error("Could not compile type constraint '" + unless ( defined $check ) { + require Moose; + Moose->throw_error( "Could not compile type constraint '" . $self->name - . "' because no constraint check"); + . "' because no constraint check" ); + } return $self->_compile_subtype($check) if $self->has_parent; @@ -181,7 +195,11 @@ sub _compile_hand_optimized_type_constraint { my $type_constraint = $self->hand_optimized_type_constraint; - Moose->throw_error("Hand optimized type constraint is not a code reference") unless ref $type_constraint; + unless ( ref $type_constraint ) { + require Moose; + Carp::confess ("Hand optimized type constraint for " . $self->name . " is not a code reference"); + Moose->throw_error("Hand optimized type constraint is not a code reference"); + } return $type_constraint; } diff --git a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm index 9ee6236..02b49eb 100644 --- a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm +++ b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm @@ -57,8 +57,10 @@ sub parameterize { if(my $parent = $self->parent) { if($parent->can('type_parameter')) { - $contained_tc->is_a_type_of($parent->type_parameter) - || Moose->throw_error("$type_parameter is not a subtype of ".$parent->type_parameter); + unless ( $contained_tc->is_a_type_of($parent->type_parameter) ) { + require Moose; + Moose->throw_error("$type_parameter is not a subtype of ".$parent->type_parameter); + } } } @@ -71,6 +73,7 @@ sub parameterize { ); } else { + require Moose; Moose->throw_error("The type parameter must be a Moose meta type"); } } diff --git a/lib/Moose/Meta/TypeConstraint/Parameterized.pm b/lib/Moose/Meta/TypeConstraint/Parameterized.pm index 1d768ce..0c10c2c 100644 --- a/lib/Moose/Meta/TypeConstraint/Parameterized.pm +++ b/lib/Moose/Meta/TypeConstraint/Parameterized.pm @@ -36,13 +36,17 @@ sub equals { sub compile_type_constraint { my $self = shift; - ($self->has_type_parameter) - || Moose->throw_error("You cannot create a Higher Order type without a type parameter"); - + unless ( $self->has_type_parameter ) { + require Moose; + Moose->throw_error("You cannot create a Higher Order type without a type parameter"); + } + my $type_parameter = $self->type_parameter; - (blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint')) - || Moose->throw_error("The type parameter must be a Moose meta type"); + unless ( blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint') ) { + require Moose; + Moose->throw_error("The type parameter must be a Moose meta type"); + } foreach my $type (Moose::Util::TypeConstraints::get_all_parameterizable_types()) { if (my $constraint = $type->generate_constraint_for($self)) { @@ -53,6 +57,7 @@ sub compile_type_constraint { # if we get here, then we couldn't # find a way to parameterize this type + require Moose; Moose->throw_error("The " . $self->name . " constraint cannot be used, because " . $self->parent->name . " doesn't subtype or coerce from a parameterizable type."); } diff --git a/lib/Moose/Meta/TypeConstraint/Registry.pm b/lib/Moose/Meta/TypeConstraint/Registry.pm index dc27c95..da978dc 100644 --- a/lib/Moose/Meta/TypeConstraint/Registry.pm +++ b/lib/Moose/Meta/TypeConstraint/Registry.pm @@ -6,7 +6,6 @@ use warnings; use metaclass; use Scalar::Util 'blessed'; -use Carp 'confess'; # FIXME Moose->throw_error our $VERSION = '0.71'; $VERSION = eval $VERSION; @@ -44,8 +43,12 @@ sub get_type_constraint { sub add_type_constraint { my ($self, $type) = @_; - confess("No type supplied / type is not a valid type constraint") - unless ($type && blessed $type && $type->isa('Moose::Meta::TypeConstraint')); + + unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) { + require Moose; + Moose->throw_error("No type supplied / type is not a valid type constraint"); + } + $self->type_constraints->{$type->name} = $type; } diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index bf3c827..aca4449 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -123,8 +123,12 @@ sub init_meta { shift; my %args = @_; - my $role = $args{for_class} - or Moose->throw_error("Cannot call init_meta without specifying a for_class"); + my $role = $args{for_class}; + + unless ($role) { + require Moose; + Moose->throw_error("Cannot call init_meta without specifying a for_class"); + } my $metaclass = $args{metaclass} || "Moose::Meta::Role"; @@ -135,8 +139,11 @@ sub init_meta { my $meta; if ($role->can('meta')) { $meta = $role->meta(); - (blessed($meta) && $meta->isa('Moose::Meta::Role')) - || Moose->throw_error("You already have a &meta function, but it does not return a Moose::Meta::Role"); + + unless ( blessed($meta) && $meta->isa('Moose::Meta::Role') ) { + require Moose; + Moose->throw_error("You already have a &meta function, but it does not return a Moose::Meta::Role"); + } } else { $meta = $metaclass->initialize($role); diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index 6955da1..7fde7d4 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -73,7 +73,10 @@ sub search_class_by_role { sub apply_all_roles { my $applicant = shift; - Moose->throw_error("Must specify at least one role to apply to $applicant") unless @_; + unless (@_) { + require Moose; + Moose->throw_error("Must specify at least one role to apply to $applicant"); + } my $roles = Data::OptList::mkopt( [@_] ); @@ -83,11 +86,16 @@ sub apply_all_roles { Class::MOP::load_class( $role_spec->[0] ); } - ( $_->[0]->can('meta') && $_->[0]->meta->isa('Moose::Meta::Role') ) - || Moose->throw_error("You can only consume roles, " - . $_->[0] - . " is not a Moose role") - foreach @$roles; + foreach my $role (@$roles) { + unless ( $role->[0]->can('meta') + && $role->[0]->meta->isa('Moose::Meta::Role') ) { + + require Moose; + Moose->throw_error( "You can only consume roles, " + . $role->[0] + . " is not a Moose role" ); + } + } if ( scalar @$roles == 1 ) { my ( $role, $params ) = @{ $roles->[0] };