Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 83575
Status: resolved
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: denis.baurain [...] ulg.ac.be
Cc:
AdminCc:

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



Subject: building lazy attributes should not harm $_
Hi, When an object with a lazy attribute is first used in a map {} operation, one may loose the first value of the input list if somewhere in the calling chain for building the attribute, some method harms the global $_. I paste below a small test case. Would it be possible to automatically shield $_ before calling the builder of a lazy attribute? Regards, Denis package Classifier; use strict; use warnings; use autodie; use Moose; has 'infile' => ( is => 'ro', isa => 'Str', required => 1, ); has '_cache' => ( traits => ['Hash'], is => 'ro', isa => 'HashRef', init_arg => undef, lazy => 1, builder => '_build_cache', handles => { classify => 'get', }, ); sub _build_cache { my $self = shift; # local $_; # uncomment this to fix the bug my %cache; open my $in, $self->infile; while (<$in>) { chomp; my ($name, $cat) = split /:/; $cache{$name} = $cat; } return \%cache; } package main; use Data::Dumper; my $classifier = Classifier->new( infile => 'cats.data' ); my @names = qw(Tom Mum Roy); my @cats = map { $classifier->classify($_) } @names; print Dumper \@names; print Dumper \@cats; # content of cats.data # Kim:family # Tom:friend # Tim:foo # Mum:family # Dad:family # Roy:foo
Subject: Re: [rt.cpan.org #83575] building lazy attributes should not harm $_
Date: Sun, 24 Feb 2013 11:09:43 -0600 (CST)
To: Denis BAURAIN via RT <bug-Moose [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Sat, 23 Feb 2013, Denis BAURAIN via RT wrote: Show quoted text
> Would it be possible to automatically shield $_ before calling the builder of a lazy attribute?
It seems like you should be doing this in the builder sub. In general, I think it's the job of the writer of a given piece of code to local-ize things like $_ and $@ as needed. -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/
Agreed. This may look stupid with my simple test case. The real bug happened to me when wrapping a non-Moose class, of which the new method messes up with $_. Once I figured out what was happening by reading the code of the wrapped class, it was easy to localize $_ in the builder of my wrapper. Maybe a warning in the section of the manual about builders would be useful? Denis Le Dim 24 Fév 2013 12:09:55, autarch@urth.org a écrit : Show quoted text
> It seems like you should be doing this in the builder sub. In general, > I > think it's the job of the writer of a given piece of code to local-ize > things like $_ and $@ as needed.
Subject: Re: [rt.cpan.org #83575] building lazy attributes should not harm $_
Date: Sun, 24 Feb 2013 17:54:04 -0600 (CST)
To: Denis BAURAIN via RT <bug-Moose [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Sun, 24 Feb 2013, Denis BAURAIN via RT wrote: Show quoted text
> The real bug happened to me when wrapping a non-Moose class, of which the new method > messes up with $_. Once I figured out what was happening by reading the code of the wrapped > class, it was easy to localize $_ in the builder of my wrapper. Maybe a warning in the section of > the manual about builders would be useful?
Yeah, I think mentioning that builders can be called anywhere, even from a map or grep, is worth including. Can you send a patch? Thanks, -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/
Added a small doc tweak in git master.