From: Shawn M Moore Date: Mon, 18 Jun 2012 23:42:01 +0000 (-0400) Subject: Convert Moose->throw_error to Moose::Util::throw X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b1a850731647b4748939ec2685b0892cf04f4307;p=gitmo%2FMoose.git Convert Moose->throw_error to Moose::Util::throw --- diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index da46de8..790407f 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -210,7 +210,7 @@ sub _parse_trait_aliases { my $name; if (ref($alias)) { reftype($alias) eq 'ARRAY' - or Moose->throw_error(reftype($alias) . " references are not " + or Moose::Util::throw(reftype($alias) . " references are not " . "valid arguments to the 'trait_aliases' " . "option"); @@ -494,8 +494,7 @@ sub _make_import_sub { _apply_meta_traits( $CALLER, $traits, $meta_lookup ); } elsif ( @{$traits} ) { - require Moose; - Moose->throw_error( + Moose::Util::throw( "Cannot provide traits when $class does not have an init_meta() method" ); } @@ -643,7 +642,7 @@ sub _apply_meta_traits { my $meta = $meta_lookup->($class); my $type = ( split /::/, ref $meta )[-1] - or Moose->throw_error( + or Moose::Util::throw( 'Cannot determine metaclass type for trait application . Meta isa ' . ref $meta ); diff --git a/lib/Moose/Meta/Attribute/Native.pm b/lib/Moose/Meta/Attribute/Native.pm index cff90f6..05ccf7a 100644 --- a/lib/Moose/Meta/Attribute/Native.pm +++ b/lib/Moose/Meta/Attribute/Native.pm @@ -11,7 +11,7 @@ for my $trait_name (@trait_names) { ); if ($meta->find_method_by_name('register_implementation')) { my $class = $meta->name->register_implementation; - Moose->throw_error( + Moose::Util::throw( "An implementation for $trait_name already exists " . "(found '$class' when trying to register '$trait_class')" ); diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 491af7c..eb31161 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -237,10 +237,10 @@ sub add_attribute { if (blessed $_[0] && ! $_[0]->isa('Moose::Meta::Role::Attribute') ) { my $class = ref $_[0]; - Moose->throw_error( "Cannot add a $class as an attribute to a role" ); + Moose::Util::throw( "Cannot add a $class as an attribute to a role" ); } elsif (!blessed($_[0]) && defined($_[0]) && $_[0] =~ /^\+(.*)/) { - Moose->throw_error( "has '+attr' is not supported in roles" ); + Moose::Util::throw( "has '+attr' is not supported in roles" ); } return $self->SUPER::add_attribute(@_); @@ -357,7 +357,7 @@ $META->add_attribute('override_method_modifiers' => ( sub add_override_method_modifier { my ($self, $method_name, $method) = @_; (!$self->has_method($method_name)) - || Moose->throw_error("Cannot add an override of method '$method_name' " . + || Moose::Util::throw("Cannot add an override of method '$method_name' " . "because there is a local version of '$method_name'"); $self->get_override_method_modifiers_map->{$method_name} = $method; } @@ -397,7 +397,7 @@ $META->add_attribute('roles' => ( sub add_role { my ($self, $role) = @_; (blessed($role) && $role->isa('Moose::Meta::Role')) - || Moose->throw_error("Roles must be instances of Moose::Meta::Role"); + || Moose::Util::throw("Roles must be instances of Moose::Meta::Role"); push @{$self->get_roles} => $role; $self->reset_package_cache_flag; } @@ -415,7 +415,7 @@ sub calculate_all_roles { sub does_role { my ($self, $role) = @_; (defined $role) - || Moose->throw_error("You must supply a role name to look for"); + || Moose::Util::throw("You must supply a role name to look for"); my $role_name = blessed $role ? $role->name : $role; # if we are it,.. then return true return 1 if $role_name eq $self->name; @@ -436,7 +436,7 @@ sub apply { my ($self, $other, %args) = @_; (blessed($other)) - || Moose->throw_error("You must pass in an blessed instance"); + || Moose::Util::throw("You must pass in an blessed instance"); my $application_class; if ($other->isa('Moose::Meta::Role')) { diff --git a/lib/Moose/Meta/Role/Application/RoleSummation.pm b/lib/Moose/Meta/Role/Application/RoleSummation.pm index c88fbd6..d07837d 100644 --- a/lib/Moose/Meta/Role/Application/RoleSummation.pm +++ b/lib/Moose/Meta/Role/Application/RoleSummation.pm @@ -78,8 +78,7 @@ sub check_role_exclusions { 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); + Moose::Util::throw(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded); } } @@ -130,8 +129,7 @@ sub apply_attributes { my $role1 = $seen{$name}->associated_role->name; my $role2 = $attr->associated_role->name; - require Moose; - Moose->throw_error( + Moose::Util::throw( "We have encountered an attribute conflict with '$name' " . "during role composition. " . " This attribute is defined in both $role1 and $role2." @@ -215,15 +213,13 @@ sub apply_override_method_modifiers { my %seen; foreach my $override (@all_overrides) { if ( $c->has_method($override->{name}) ){ - require Moose; - Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " . + Moose::Util::throw( "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}}) { if ( $seen{$override->{name}} != $override->{method} ) { - require Moose; - Moose->throw_error( "We have encountered an 'override' method conflict during " . + Moose::Util::throw( "We have 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/Application/ToRole.pm b/lib/Moose/Meta/Role/Application/ToRole.pm index e8ab886..33ee5df 100644 --- a/lib/Moose/Meta/Role/Application/ToRole.pm +++ b/lib/Moose/Meta/Role/Application/ToRole.pm @@ -17,13 +17,11 @@ sub apply { sub check_role_exclusions { my ($self, $role1, $role2) = @_; if ( $role2->excludes_role($role1->name) ) { - require Moose; - Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'"); + Moose::Util::throw("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'"); } foreach my $excluded_role_name ($role1->get_excluded_roles_list) { if ( $role2->does_role($excluded_role_name) ) { - require Moose; - Moose->throw_error("The role " . $role2->name . " does the excluded role '$excluded_role_name'"); + Moose::Util::throw("The role " . $role2->name . " does the excluded role '$excluded_role_name'"); } $role2->add_excluded_roles($excluded_role_name); } @@ -55,8 +53,7 @@ sub apply_attributes { my $role2_name = $role2->name; - require Moose; - Moose->throw_error( "Role '" + Moose::Util::throw( "Role '" . $role1->name . "' has encountered an attribute conflict" . " while being composed into '$role2_name'." @@ -109,8 +106,7 @@ sub apply_methods { if ( $role2_method && $role2_method->body != $method->body ) { - require Moose; - Moose->throw_error( + Moose::Util::throw( "Cannot create a method alias if a local method of the same name exists" ); } @@ -136,8 +132,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 " . + Moose::Util::throw("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."); } @@ -148,8 +143,7 @@ sub apply_override_method_modifiers { 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 " . + Moose::Util::throw("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 0e90e4e..8937e0a 100644 --- a/lib/Moose/Meta/Role/Composite.pm +++ b/lib/Moose/Meta/Role/Composite.pm @@ -44,8 +44,7 @@ sub new { # the roles param is required ... 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 $_"); + Moose::Util::throw("The list of roles must be instances of Moose::Meta::Role, not $_"); } } @@ -76,7 +75,7 @@ sub add_method { my ($self, $method_name, $method) = @_; unless ( defined $method_name && $method_name ) { - Moose->throw_error("You must define a method name"); + Moose::Util::throw("You must define a method name"); } my $body; @@ -133,7 +132,7 @@ sub apply_params { sub reinitialize { my ( $class, $old_meta, @args ) = @_; - Moose->throw_error( + Moose::Util::throw( 'Moose::Meta::Role::Composite instances can only be reinitialized from an existing metaclass instance' ) if !blessed $old_meta diff --git a/lib/Moose/Meta/TypeCoercion.pm b/lib/Moose/Meta/TypeCoercion.pm index 6ecafba..2b2a0ec 100644 --- a/lib/Moose/Meta/TypeCoercion.pm +++ b/lib/Moose/Meta/TypeCoercion.pm @@ -44,8 +44,7 @@ sub compile_type_coercion { my $type_constraint = ref $constraint_name ? $constraint_name : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name); unless ( defined $type_constraint ) { - require Moose; - Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from"); + Moose::Util::throw("Could not find the type constraint ($constraint_name) to coerce from"); } push @coercions => [ @@ -82,8 +81,7 @@ sub add_type_coercions { my ($constraint_name, $action) = splice(@new_coercion_map, 0, 2); if ( exists $has_coercion{$constraint_name} ) { - require Moose; - Moose->throw_error("A coercion action already exists for '$constraint_name'") + Moose::Util::throw("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 6e6f7da..cdd9d72 100644 --- a/lib/Moose/Meta/TypeCoercion/Union.pm +++ b/lib/Moose/Meta/TypeCoercion/Union.pm @@ -14,7 +14,7 @@ sub compile_type_coercion { my $type_constraint = $self->type_constraint; (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Union')) - || Moose->throw_error("You can only create a Moose::Meta::TypeCoercion::Union for a " . + || Moose::Util::throw("You can only create a Moose::Meta::TypeCoercion::Union for a " . "Moose::Meta::TypeConstraint::Union, not a $type_constraint"); $self->_compiled_type_coercion( @@ -35,8 +35,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"); + Moose::Util::throw("Cannot add additional type coercions to Union types"); } 1; diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index c8d76b5..1508874 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -150,8 +150,7 @@ sub coerce { my $coercion = $self->coercion; unless ($coercion) { - require Moose; - Moose->throw_error("Cannot coerce without a type coercion"); + Moose::Util::throw("Cannot coerce without a type coercion"); } return $_[0] if $self->check($_[0]); @@ -165,8 +164,7 @@ sub assert_coerce { my $coercion = $self->coercion; unless ($coercion) { - require Moose; - Moose->throw_error("Cannot coerce without a type coercion"); + Moose::Util::throw("Cannot coerce without a type coercion"); } return $_[0] if $self->check($_[0]); @@ -208,8 +206,7 @@ sub _inline_check { my $self = shift; unless ( $self->can_be_inlined ) { - require Moose; - Moose->throw_error( 'Cannot inline a type constraint check for ' . $self->name ); + Moose::Util::throw( 'Cannot inline a type constraint check for ' . $self->name ); } if ( $self->has_parent && $self->constraint == $null_constraint ) { @@ -324,8 +321,7 @@ sub _actually_compile_type_constraint { my $check = $self->constraint; unless ( defined $check ) { - require Moose; - Moose->throw_error( "Could not compile type constraint '" + Moose::Util::throw( "Could not compile type constraint '" . $self->name . "' because no constraint check" ); } @@ -342,8 +338,7 @@ sub _compile_hand_optimized_type_constraint { my $type_constraint = $self->hand_optimized_type_constraint; unless ( ref $type_constraint ) { - require Moose; - Moose->throw_error("Hand optimized type constraint is not a code reference"); + Moose::Util::throw("Hand optimized type constraint is not a code reference"); } return $type_constraint; diff --git a/lib/Moose/Meta/TypeConstraint/Enum.pm b/lib/Moose/Meta/TypeConstraint/Enum.pm index 3ef7234..0f876e7 100644 --- a/lib/Moose/Meta/TypeConstraint/Enum.pm +++ b/lib/Moose/Meta/TypeConstraint/Enum.pm @@ -37,18 +37,15 @@ sub new { $args{inlined} = $inliner; if ( scalar @{ $args{values} } < 1 ) { - require Moose; - Moose->throw_error("You must have at least one value to enumerate through"); + Moose::Util::throw("You must have at least one value to enumerate through"); } for (@{ $args{values} }) { if (!defined($_)) { - require Moose; - Moose->throw_error("Enum values must be strings, not undef"); + Moose::Util::throw("Enum values must be strings, not undef"); } elsif (ref($_)) { - require Moose; - Moose->throw_error("Enum values must be strings, not '$_'"); + Moose::Util::throw("Enum values must be strings, not '$_'"); } } diff --git a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm index 592bc8d..4706cc7 100644 --- a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm +++ b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm @@ -7,6 +7,7 @@ use metaclass; use base 'Moose::Meta::TypeConstraint'; use Moose::Meta::TypeConstraint::Parameterized; use Moose::Util::TypeConstraints (); +use Moose::Util (); __PACKAGE__->meta->add_attribute('constraint_generator' => ( accessor => 'constraint_generator', @@ -71,8 +72,7 @@ sub parameterize { if(my $parent = $self->parent) { if($parent->can('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); + Moose::Util::throw("$type_parameter is not a subtype of ".$parent->type_parameter); } } } @@ -87,8 +87,7 @@ sub parameterize { ); } else { - require Moose; - Moose->throw_error("The type parameter must be a Moose meta type"); + Moose::Util::throw("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 175c9ab..71731c5 100644 --- a/lib/Moose/Meta/TypeConstraint/Parameterized.pm +++ b/lib/Moose/Meta/TypeConstraint/Parameterized.pm @@ -40,15 +40,13 @@ sub compile_type_constraint { my $self = shift; unless ( $self->has_type_parameter ) { - require Moose; - Moose->throw_error("You cannot create a Higher Order type without a type parameter"); + Moose::Util::throw("You cannot create a Higher Order type without a type parameter"); } my $type_parameter = $self->type_parameter; unless ( blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint') ) { - require Moose; - Moose->throw_error("The type parameter must be a Moose meta type"); + Moose::Util::throw("The type parameter must be a Moose meta type"); } foreach my $type (Moose::Util::TypeConstraints::get_all_parameterizable_types()) { @@ -60,8 +58,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 " + Moose::Util::throw("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 59105d1..d9cc3d8 100644 --- a/lib/Moose/Meta/TypeConstraint/Registry.pm +++ b/lib/Moose/Meta/TypeConstraint/Registry.pm @@ -43,8 +43,7 @@ sub add_type_constraint { my ($self, $type) = @_; unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) { - require Moose; - Moose->throw_error("No type supplied / type is not a valid type constraint"); + Moose::Util::throw("No type supplied / type is not a valid type constraint"); } $self->type_constraints->{$type->name} = $type; diff --git a/lib/Moose/Object.pm b/lib/Moose/Object.pm index e619d1f..b19bdab 100644 --- a/lib/Moose/Object.pm +++ b/lib/Moose/Object.pm @@ -9,6 +9,7 @@ use Devel::GlobalDestruction (); use MRO::Compat (); use Scalar::Util (); use Try::Tiny (); +use Moose::Util (); use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class'; use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class'; diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index 48e40b7..ef6ec06 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -100,17 +100,16 @@ sub init_meta { my $role = $args{for_class}; unless ($role) { - require Moose; - Moose->throw_error("Cannot call init_meta without specifying a for_class"); + Moose::Util::throw("Cannot call init_meta without specifying a for_class"); } my $metaclass = $args{metaclass} || "Moose::Meta::Role"; my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta'; - Moose->throw_error("The Metaclass $metaclass must be loaded. (Perhaps you forgot to 'use $metaclass'?)") + Moose::Util::throw("The Metaclass $metaclass must be loaded. (Perhaps you forgot to 'use $metaclass'?)") unless is_class_loaded($metaclass); - Moose->throw_error("The Metaclass $metaclass must be a subclass of Moose::Meta::Role.") + Moose::Util::throw("The Metaclass $metaclass must be a subclass of Moose::Meta::Role.") unless $metaclass->isa('Moose::Meta::Role'); # make a subtype for each Moose role @@ -121,9 +120,9 @@ sub init_meta { unless ( $meta->isa("Moose::Meta::Role") ) { my $error_message = "$role already has a metaclass, but it does not inherit $metaclass ($meta)."; if ( $meta->isa('Moose::Meta::Class') ) { - Moose->throw_error($error_message . ' You cannot make the same thing a role and a class. Remove either Moose or Moose::Role.'); + Moose::Util::throw($error_message . ' You cannot make the same thing a role and a class. Remove either Moose or Moose::Role.'); } else { - Moose->throw_error($error_message); + Moose::Util::throw($error_message); } } }