From: Dave Rolsky Date: Thu, 13 Aug 2009 15:46:38 +0000 (-0500) Subject: Use double-quotes and quotemeta for inlined hash slot access. X-Git-Tag: 0.92~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5143ee94fd91fcc17dfb6428baa503c487df2665;p=gitmo%2FClass-MOP.git Use double-quotes and quotemeta for inlined hash slot access. This seems safer than using regex substitutions to escape strings. --- diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index d11b1b6..c32d4e1 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -181,8 +181,7 @@ sub inline_create_instance { sub inline_slot_access { my ($self, $instance, $slot_name) = @_; - $slot_name =~ s/(['\\])/\\$1/g; # In '', only "'" and "\\" are meta characters. - sprintf q[%s->{'%s'}], $instance, $slot_name; + sprintf q[%s->{"%s"}], $instance, quotemeta($slot_name); } sub inline_get_slot_value { diff --git a/t/061_instance_inline.t b/t/061_instance_inline.t index 3856bfe..0141945 100644 --- a/t/061_instance_inline.t +++ b/t/061_instance_inline.t @@ -18,11 +18,11 @@ my $C = 'Class::MOP::Instance'; 'bless {} => $class', '... got the right code for create_instance'); is($C->inline_get_slot_value($instance, $slot_name), - "\$self->{'foo'}", + q[$self->{"foo"}], '... got the right code for get_slot_value'); is($C->inline_set_slot_value($instance, $slot_name, $value), - "\$self->{'foo'} = \$value", + q[$self->{"foo"} = $value], '... got the right code for set_slot_value'); is($C->inline_initialize_slot($instance, $slot_name), @@ -30,18 +30,18 @@ my $C = 'Class::MOP::Instance'; '... got the right code for initialize_slot'); is($C->inline_is_slot_initialized($instance, $slot_name), - "exists \$self->{'foo'}", + q[exists $self->{"foo"}], '... got the right code for get_slot_value'); is($C->inline_weaken_slot_value($instance, $slot_name), - "Scalar::Util::weaken( \$self->{'foo'} )", + q[Scalar::Util::weaken( $self->{"foo"} )], '... got the right code for weaken_slot_value'); is($C->inline_strengthen_slot_value($instance, $slot_name), - "\$self->{'foo'} = \$self->{'foo'}", + q[$self->{"foo"} = $self->{"foo"}], '... got the right code for strengthen_slot_value'); is($C->inline_rebless_instance_structure($instance, $class), - "bless \$self => \$class", + q[bless $self => $class], '... got the right code for rebless_instance_structure'); }