[Moose-commits] r7750 - in Moose/trunk/lib/Moose: . Meta Meta/Method Meta/Role Meta/Role/Application Meta/TypeCoercion Meta/TypeConstraint

autarch at code2.0beta.co.uk autarch at code2.0beta.co.uk
Fri Feb 20 18:06:54 GMT 2009


Author: autarch
Date: 2009-02-20 10:06:53 -0800 (Fri, 20 Feb 2009)
New Revision: 7750

Modified:
   Moose/trunk/lib/Moose/Exporter.pm
   Moose/trunk/lib/Moose/Meta/Method/Constructor.pm
   Moose/trunk/lib/Moose/Meta/Role.pm
   Moose/trunk/lib/Moose/Meta/Role/Application/RoleSummation.pm
   Moose/trunk/lib/Moose/Meta/Role/Application/ToRole.pm
   Moose/trunk/lib/Moose/Meta/Role/Composite.pm
   Moose/trunk/lib/Moose/Meta/TypeCoercion.pm
   Moose/trunk/lib/Moose/Meta/TypeCoercion/Union.pm
   Moose/trunk/lib/Moose/Meta/TypeConstraint.pm
   Moose/trunk/lib/Moose/Meta/TypeConstraint/Parameterizable.pm
   Moose/trunk/lib/Moose/Meta/TypeConstraint/Parameterized.pm
   Moose/trunk/lib/Moose/Meta/TypeConstraint/Registry.pm
   Moose/trunk/lib/Moose/Role.pm
   Moose/trunk/lib/Moose/Util.pm
Log:
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.

Modified: Moose/trunk/lib/Moose/Exporter.pm
===================================================================
--- Moose/trunk/lib/Moose/Exporter.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Exporter.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -262,6 +262,7 @@
             _apply_meta_traits( $CALLER, $traits );
         }
         elsif ( @{$traits} ) {
+            require Moose;
             Moose->throw_error(
                 "Cannot provide traits when $class does not have an init_meta() method"
             );

Modified: Moose/trunk/lib/Moose/Meta/Method/Constructor.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/Method/Constructor.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/Method/Constructor.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -117,7 +117,7 @@
 
 # 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;

Modified: Moose/trunk/lib/Moose/Meta/Role/Application/RoleSummation.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/Role/Application/RoleSummation.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/Role/Application/RoleSummation.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -79,6 +79,8 @@
             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 @@
     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 @@
     
     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};
     }

Modified: Moose/trunk/lib/Moose/Meta/Role/Application/ToRole.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/Role/Application/ToRole.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/Role/Application/ToRole.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -20,11 +20,15 @@
 
 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 @@
         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 @@
             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 @@
             # 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 @@
             # 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.");

Modified: Moose/trunk/lib/Moose/Meta/Role/Composite.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/Role/Composite.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/Role/Composite.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -33,9 +33,12 @@
 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,9 +51,11 @@
 # 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)) {
         $body = $method->body;

Modified: Moose/trunk/lib/Moose/Meta/Role.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/Role.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/Role.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -122,8 +122,10 @@
 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];

Modified: Moose/trunk/lib/Moose/Meta/TypeCoercion/Union.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/TypeCoercion/Union.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/TypeCoercion/Union.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -43,6 +43,7 @@
 sub has_coercion_for_type { 0 }
 
 sub add_type_coercions {
+    require Moose;
     Moose->throw_error("Cannot add additional type coercions to Union types");
 }
 

Modified: Moose/trunk/lib/Moose/Meta/TypeCoercion.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/TypeCoercion.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/TypeCoercion.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -43,8 +43,12 @@
     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 @@
     
     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);
     }
     

Modified: Moose/trunk/lib/Moose/Meta/TypeConstraint/Parameterizable.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/TypeConstraint/Parameterizable.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/TypeConstraint/Parameterizable.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -57,8 +57,10 @@
     
     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 @@
         );
     }
     else {
+        require Moose;
         Moose->throw_error("The type parameter must be a Moose meta type");
     }
 }

Modified: Moose/trunk/lib/Moose/Meta/TypeConstraint/Parameterized.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/TypeConstraint/Parameterized.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/TypeConstraint/Parameterized.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -36,13 +36,17 @@
 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 @@
     
     # 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.");
 }

Modified: Moose/trunk/lib/Moose/Meta/TypeConstraint/Registry.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/TypeConstraint/Registry.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/TypeConstraint/Registry.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -6,7 +6,6 @@
 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 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;
 }
 

Modified: Moose/trunk/lib/Moose/Meta/TypeConstraint.pm
===================================================================
--- Moose/trunk/lib/Moose/Meta/TypeConstraint.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Meta/TypeConstraint.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -36,6 +36,7 @@
     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 @@
     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,8 +72,19 @@
 
 
 
-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) = @_;
     my $constraint_subref = $self->_compiled_type_constraint;
@@ -165,10 +177,12 @@
         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 @@
 
     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;
 }

Modified: Moose/trunk/lib/Moose/Role.pm
===================================================================
--- Moose/trunk/lib/Moose/Role.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Role.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -123,9 +123,13 @@
     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";
 
     # make a subtype for each Moose class
@@ -135,8 +139,11 @@
     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);

Modified: Moose/trunk/lib/Moose/Util.pm
===================================================================
--- Moose/trunk/lib/Moose/Util.pm	2009-02-20 17:40:29 UTC (rev 7749)
+++ Moose/trunk/lib/Moose/Util.pm	2009-02-20 18:06:53 UTC (rev 7750)
@@ -73,7 +73,10 @@
 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,12 +86,17 @@
         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] };
         $role->meta->apply( $meta, ( defined $params ? %$params : () ) );




More information about the Moose-commits mailing list