From: Jesse Luehrs Date: Tue, 4 Jan 2011 17:55:41 +0000 (-0600) Subject: don't initialize lazy attrs with defaults in the constructor (mo) X-Git-Tag: 1.9903~37 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=545c60127ba7a6954143eeb4a3e33bb4fbd91342;p=gitmo%2FMoose.git don't initialize lazy attrs with defaults in the constructor (mo) --- diff --git a/Changes b/Changes index af69385..d8bb65c 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,11 @@ for, noteworthy changes. {{$NEXT}} + [BUG FIXES] + + * Don't initialize lazy attributes with defaults in the constructor (for + immutable classes). (mo) + 1.9902-TRIAL Mon, Jan 03, 2011 [OTHER] diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index fa55548..4ad1108 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -389,6 +389,7 @@ sub _inline_init_attr_from_default { my $self = shift; my ($attr, $idx) = @_; + return if $attr->can('is_lazy') && $attr->is_lazy; my $default = $self->_inline_default_value($attr, $idx); return unless $default; diff --git a/t/300_immutable/012_default_values.t b/t/300_immutable/012_default_values.t index 2b9ab02..9998400 100644 --- a/t/300_immutable/012_default_values.t +++ b/t/300_immutable/012_default_values.t @@ -35,6 +35,8 @@ is( $foo->faz, qq{\0}, # Lazy attrs were never broken, but it doesn't hurt to test that they # won't be broken by any future changes. +# Also make sure that attributes stay lazy even after being immutable + { package Bar; @@ -46,7 +48,22 @@ is( $foo->faz, qq{\0}, has 'buz' => ( is => 'rw', default => q{"'\\}, lazy => 1 ); has 'faz' => ( is => 'rw', default => qq{\0}, lazy => 1 ); + { + my $bar = Bar->new; + ::ok(!$bar->meta->get_attribute($_)->has_value($bar), + "Attribute $_ has no value") + for qw(foo bar baz buz faz); + } + ::is( ::exception { __PACKAGE__->meta->make_immutable }, undef, 'no errors making a package immutable when it has lazy default values that could break quoting' ); + + { + my $bar = Bar->new; + ::ok(!$bar->meta->get_attribute($_)->has_value($bar), + "Attribute $_ has no value (immutable)") + for(qw(foo bar baz buz faz)); + } + } my $bar = Bar->new;