Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Mouse CPAN distribution.

Report information
The Basics
Id: 73592
Status: resolved
Priority: 0/
Queue: Mouse

People
Owner: Nobody in particular
Requestors: 'spro^^*%*^6ut# [...] &$%*c
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Use of local $_
Due to a bug in perl, local($_) is practically unusable, because if the caller happens to have tied $_, it ends up messing with the tied variable. You may be wondering why anyone would tie $_, but it is quite easy to alias $_ to a tied variable without realising it. Mouse is using local $_ in three places: $ ack 'local\s*\(?\s*\$_' lib/Mouse/Meta/TypeConstraint.pm 152: local $_ = $thing; 193: local $_ = $value; lib/Mouse/PurePerl.pm 657: local $_ = $args[0]; Each of those can (and should) be changed to for(...) { ... }. (See also <https://rt.perl.org/rt3/Ticket/Display.html?id=105912> and <https://rt.cpan.org/Ticket/Display.html?id=73543>.)
Could you write a test case?
On Mon Jan 02 02:25:47 2012, GFUJI wrote: Show quoted text
> Could you write a test case?
Attached is a test case with a fix, too. :-)
Subject: open_bwIulpB6.txt
diff -Nurp Mouse-0.97-ZRkQgT/MANIFEST Mouse-0.97-ZRkQgT-copy/MANIFEST --- Mouse-0.97-ZRkQgT/MANIFEST 2011-10-03 17:11:08.000000000 -0700 +++ Mouse-0.97-ZRkQgT-copy/MANIFEST 2012-01-02 09:34:09.000000000 -0800 @@ -345,6 +345,7 @@ t/900_mouse_bugs/009_RT57144.t t/900_mouse_bugs/010_use_mouse_before_tb2.t t/900_mouse_bugs/011_RT61852.t t/900_mouse_bugs/012_RT61906.t +t/900_mouse_bugs/013_RT73592_tied_.t t/990_deprecated/001-export_to_level.t t/lib/Bar.pm t/lib/Bar7/Meta/Trait.pm diff -Nurp Mouse-0.97-ZRkQgT/lib/Mouse/Meta/TypeConstraint.pm Mouse-0.97-ZRkQgT-copy/lib/Mouse/Meta/TypeConstraint.pm --- Mouse-0.97-ZRkQgT/lib/Mouse/Meta/TypeConstraint.pm 2011-10-09 14:46:03.000000000 -0700 +++ Mouse-0.97-ZRkQgT-copy/lib/Mouse/Meta/TypeConstraint.pm 2012-01-02 09:28:30.000000000 -0800 @@ -149,8 +149,7 @@ sub _compiled_type_coercion { foreach my $pair (@coercions) { #my ($constraint, $converter) = @$pair; if ($pair->[0]->($thing)) { - local $_ = $thing; - return $pair->[1]->($thing); + return $pair->[1]->($thing) for $thing; } } return $thing; @@ -190,8 +189,7 @@ sub coerce { sub get_message { my ($self, $value) = @_; if ( my $msg = $self->message ) { - local $_ = $value; - return $msg->($value); + return $msg->($value) for $value; } else { if(not defined $value) { diff -Nurp Mouse-0.97-ZRkQgT/lib/Mouse/PurePerl.pm Mouse-0.97-ZRkQgT-copy/lib/Mouse/PurePerl.pm --- Mouse-0.97-ZRkQgT/lib/Mouse/PurePerl.pm 2011-10-09 14:46:03.000000000 -0700 +++ Mouse-0.97-ZRkQgT-copy/lib/Mouse/PurePerl.pm 2012-01-02 09:35:15.000000000 -0800 @@ -653,12 +653,13 @@ sub compile_type_constraint{ } else{ $self->{compiled_type_constraint} = sub{ - my(@args) = @_; - local $_ = $args[0]; + my(@args) = @_; + for ($args[0]) { foreach my $c(@checks){ return undef if !$c->(@args); } - return 1; + } + return 1; }; } return; diff -Nurp Mouse-0.97-ZRkQgT/t/900_mouse_bugs/013_RT73592_tied_.t Mouse-0.97-ZRkQgT-copy/t/900_mouse_bugs/013_RT73592_tied_.t --- Mouse-0.97-ZRkQgT/t/900_mouse_bugs/013_RT73592_tied_.t 1969-12-31 16:00:00.000000000 -0800 +++ Mouse-0.97-ZRkQgT-copy/t/900_mouse_bugs/013_RT73592_tied_.t 2012-01-02 09:35:31.000000000 -0800 @@ -0,0 +1,56 @@ +#!perl +# https://rt.cpan.org/Ticket/Display.html?id=73592 +use Test::More tests => 2; + +sub TIESCALAR { bless [] } +# Load Carp before tying as it uses Exporter, and Exporter < 5.66 has the +# local $_ bug. +require Carp; +eval { require Carp::Heavy }; +tie $_, ""; + +{ + package Human; + + use Mouse; + use Mouse::Util::TypeConstraints; + + coerce 'Human::EyeColor' + => from 'ArrayRef' + => via { return Human::EyeColor->new(); }; + + has 'eye_color' => ( + is => 'ro', + isa => 'Human::EyeColor', + coerce => 1, + ); + + subtype 'NonemptyStr' + => as 'Str' + => where { length $_ } + => message { "The string is empty!" }; + + has name => ( + is => 'ro', + isa => 'NonemptyStr', + ); +} + +{ + package Human::EyeColor; + + use Mouse; +} + +ok eval { + my $person = Human->new( + eye_color => [ qw( blue blue blue blue ) ], + ); + 1 + }, 'coercion does not interfere with $_'; + +eval { + my $person = Human->new(name => ''); +}; +like $@, qr/The string is empty/, + 'type constraint messages do not interfere with $_';
Sorry to being late. Fixed in 0.99. -- Goro Fuji (gfx) GFUJI at CPAN.org
Sorry to being late. Fixed in 0.99. -- Goro Fuji (gfx) GFUJI at CPAN.org