From: Yuval Kogman Date: Mon, 11 Aug 2008 02:39:32 +0000 (+0000) Subject: use ref instead of blessed where it's not needed, and other small nits X-Git-Tag: 0_64_01~37 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b3fa93c79abf0311c96906e55e3f83e0574c9cad;p=gitmo%2FClass-MOP.git use ref instead of blessed where it's not needed, and other small nits --- diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index 4be906d..496050d 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -81,7 +81,7 @@ sub clone { my %options = @_; (blessed($self)) || confess "Can only clone an instance"; - return bless { %{$self}, %options } => blessed($self); + return bless { %{$self}, %options } => ref($self); } sub initialize_instance_slot { @@ -115,7 +115,7 @@ sub initialize_instance_slot { ); } else { - confess(blessed($instance)." does not support builder method '". $self->{'builder'} ."' for attribute '" . $self->name . "'"); + confess(ref($instance)." does not support builder method '". $self->{'builder'} ."' for attribute '" . $self->name . "'"); } } } @@ -147,15 +147,15 @@ sub name { $_[0]->{'name'} } sub associated_class { $_[0]->{'associated_class'} } sub associated_methods { $_[0]->{'associated_methods'} } -sub has_accessor { defined($_[0]->{'accessor'}) ? 1 : 0 } -sub has_reader { defined($_[0]->{'reader'}) ? 1 : 0 } -sub has_writer { defined($_[0]->{'writer'}) ? 1 : 0 } -sub has_predicate { defined($_[0]->{'predicate'}) ? 1 : 0 } -sub has_clearer { defined($_[0]->{'clearer'}) ? 1 : 0 } -sub has_builder { defined($_[0]->{'builder'}) ? 1 : 0 } -sub has_init_arg { defined($_[0]->{'init_arg'}) ? 1 : 0 } -sub has_default { defined($_[0]->{'default'}) ? 1 : 0 } -sub has_initializer { defined($_[0]->{'initializer'}) ? 1 : 0 } +sub has_accessor { defined($_[0]->{'accessor'}) } +sub has_reader { defined($_[0]->{'reader'}) } +sub has_writer { defined($_[0]->{'writer'}) } +sub has_predicate { defined($_[0]->{'predicate'}) } +sub has_clearer { defined($_[0]->{'clearer'}) } +sub has_builder { defined($_[0]->{'builder'}) } +sub has_init_arg { defined($_[0]->{'init_arg'}) } +sub has_default { defined($_[0]->{'default'}) } +sub has_initializer { defined($_[0]->{'initializer'}) } sub accessor { $_[0]->{'accessor'} } sub reader { $_[0]->{'reader'} } @@ -278,7 +278,7 @@ sub associate_method { sub set_initial_value { my ($self, $instance, $value) = @_; $self->_set_initial_slot_value( - Class::MOP::Class->initialize(blessed($instance))->get_meta_instance, + Class::MOP::Class->initialize(ref($instance))->get_meta_instance, $instance, $value ); @@ -287,7 +287,7 @@ sub set_initial_value { sub set_value { my ($self, $instance, $value) = @_; - Class::MOP::Class->initialize(blessed($instance)) + Class::MOP::Class->initialize(ref($instance)) ->get_meta_instance ->set_slot_value($instance, $self->name, $value); } @@ -295,7 +295,7 @@ sub set_value { sub get_value { my ($self, $instance) = @_; - Class::MOP::Class->initialize(blessed($instance)) + Class::MOP::Class->initialize(ref($instance)) ->get_meta_instance ->get_slot_value($instance, $self->name); } @@ -303,7 +303,7 @@ sub get_value { sub has_value { my ($self, $instance) = @_; - Class::MOP::Class->initialize(blessed($instance)) + Class::MOP::Class->initialize(ref($instance)) ->get_meta_instance ->is_slot_initialized($instance, $self->name); } @@ -311,7 +311,7 @@ sub has_value { sub clear_value { my ($self, $instance) = @_; - Class::MOP::Class->initialize(blessed($instance)) + Class::MOP::Class->initialize(ref($instance)) ->get_meta_instance ->deinitialize_slot($instance, $self->name); } @@ -388,7 +388,7 @@ sub install_accessors { } my $method = $class->get_method($accessor); $class->remove_method($accessor) - if (blessed($method) && $method->isa('Class::MOP::Method::Accessor')); + if (ref($method) && $method->isa('Class::MOP::Method::Accessor')); }; sub remove_accessors { diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index da66d76..79c07b4 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -72,10 +72,10 @@ sub construct_class_instance { # we need to deal with the possibility # of class immutability here, and then # get the name of the class appropriately - $class = (blessed($class) + $class = (ref($class) ? ($class->is_immutable ? $class->get_mutable_metaclass_name() - : blessed($class)) + : ref($class)) : $class); # now create the metaclass @@ -154,7 +154,7 @@ sub check_metaclass_compatability { my $self = shift; # this is always okay ... - return if blessed($self) eq 'Class::MOP::Class' && + return if ref($self) eq 'Class::MOP::Class' && $self->instance_metaclass eq 'Class::MOP::Instance'; my @class_list = $self->linearized_isa; @@ -169,10 +169,10 @@ sub check_metaclass_compatability { # get the name of the class appropriately my $meta_type = ($meta->is_immutable ? $meta->get_mutable_metaclass_name() - : blessed($meta)); + : ref($meta)); ($self->isa($meta_type)) - || confess $self->name . "->meta => (" . (blessed($self)) . ")" . + || confess $self->name . "->meta => (" . (ref($self)) . ")" . " is not compatible with the " . $class_name . "->meta => (" . ($meta_type) . ")"; # NOTE: @@ -204,7 +204,7 @@ sub check_metaclass_compatability { sub is_anon_class { my $self = shift; no warnings 'uninitialized'; - $self->name =~ /^$ANON_CLASS_PREFIX/ ? 1 : 0; + $self->name =~ /^$ANON_CLASS_PREFIX/; } sub create_anon_class { @@ -283,7 +283,7 @@ sub create { # FIXME totally lame $meta->add_method('meta' => sub { - $class->initialize(blessed($_[0]) || $_[0]); + $class->initialize(ref($_[0]) || $_[0]); }); $meta->superclasses(@{$options{superclasses}}) @@ -430,7 +430,7 @@ sub clone_object { my $class = shift; my $instance = shift; (blessed($instance) && $instance->isa($class->name)) - || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)"; + || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)"; # NOTE: # we need to protect the integrity of the @@ -466,7 +466,7 @@ sub rebless_instance { $old_metaclass = $instance->meta; } else { - $old_metaclass = $self->initialize(blessed($instance)); + $old_metaclass = $self->initialize(ref($instance)); } my $meta_instance = $self->get_meta_instance(); @@ -946,7 +946,7 @@ sub has_attribute { my ($self, $attribute_name) = @_; (defined $attribute_name && $attribute_name) || confess "You must define an attribute name"; - exists $self->get_attribute_map->{$attribute_name} ? 1 : 0; + exists $self->get_attribute_map->{$attribute_name}; } sub get_attribute { @@ -1040,7 +1040,7 @@ sub is_immutable { 0 } sub get_immutable_transformer { my $self = shift; if( $self->is_mutable ){ - my $class = blessed $self || $self; + my $class = ref $self || $self; return $IMMUTABLE_TRANSFORMERS{$class} ||= $self->create_immutable_transformer; } confess "unable to find transformer for immutable class" diff --git a/lib/Class/MOP/Package.pm b/lib/Class/MOP/Package.pm index 4b9e551..0d6dc1a 100644 --- a/lib/Class/MOP/Package.pm +++ b/lib/Class/MOP/Package.pm @@ -127,14 +127,14 @@ sub has_package_symbol { # then this is broken. if (ref($namespace->{$name}) eq 'SCALAR') { - return ($type eq 'CODE' ? 1 : 0); + return ($type eq 'CODE'); } elsif ($type eq 'SCALAR') { my $val = *{$namespace->{$name}}{$type}; - return defined(${$val}) ? 1 : 0; + return defined(${$val}); } else { - defined(*{$namespace->{$name}}{$type}) ? 1 : 0; + defined(*{$namespace->{$name}}{$type}); } }