Skip Menu |
 

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 118305
Status: open
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: happy.barney [...] gmail.com
Cc:
AdminCc:

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



Subject: Array map/grep method allows to change value without constraint check
Date: Sun, 9 Oct 2016 01:53:31 +0200
To: bug-Moose [...] rt.cpan.org
From: Branislav Zahradník <happy.barney [...] gmail.com>
Download (untitled) / with headers
text/plain 419b
If code passed to native trait map/grep assigns value to $_, value is stored but constraints are not verified. Snippet: package Foo; has list => is => 'ro', isa => 'ArrayRef[Defined]', handles => { map => 'map' }; Usage; my $foo = Foo->new( list => [1 .. 10] ); $foo->map( sub { $_ = undef } ); Solution: return 'map { local $_ = $_; $_[0]->() } @{ (' . $slot_access . ') }'; Best regards, Branislav Zahradník
Download (untitled) / with headers
text/plain 471b
On 2016-10-08 16:53:40, happy.barney@gmail.com wrote: Show quoted text
> If code passed to native trait map/grep assigns value to $_, value is > stored but constraints are not verified.
I don't think this is fixable. You're altering the contents of the array elements via a reference. Moose can't see what you're doing there, so you can change the values to anything. The only alternative is to make a full copy of the list before allowing you to traverse it, which would get expensive.
Subject: Re: [rt.cpan.org #118305] Array map/grep method allows to change value without constraint check
Date: Sun, 9 Oct 2016 02:19:24 +0200
To: bug-Moose [...] rt.cpan.org
From: Branislav Zahradník <happy.barney [...] gmail.com>
Download (untitled) / with headers
text/plain 827b
well, that's true, if element is a reference, there is no chance to detect it except rewrite map to Writer. using "local $_ = $_" prevents at least assign to $_ changes (often unintended) On 9 October 2016 at 02:01, Karen Etheridge via RT <bug-Moose@rt.cpan.org> wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=118305 > > > On 2016-10-08 16:53:40, happy.barney@gmail.com wrote:
> > If code passed to native trait map/grep assigns value to $_, value is > > stored but constraints are not verified.
> > I don't think this is fixable. You're altering the contents of the array > elements via a reference. Moose can't see what you're doing there, so you > can change the values to anything. > > The only alternative is to make a full copy of the list before allowing > you to traverse it, which would get expensive. >
Download (untitled) / with headers
text/plain 454b
On 2016-10-08 17:19:33, happy.barney@gmail.com wrote: Show quoted text
> well, that's true, if element is a reference, there is no chance to detect > it except rewrite map to Writer.
It doesn't what matter the type of the elements are. Show quoted text
> using "local $_ = $_" prevents at least assign to $_ changes (often > unintended)
Indeed. This could potentially be added as a new type of delegate accessor, but I don't think we can change the existing ones without breaking code.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.