From: Shawn M Moore Date: Wed, 1 Apr 2009 03:33:15 +0000 (-0400) Subject: Forbid undef passed to new for Moose compat X-Git-Tag: 0.20~49 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=c9aefe2628c559c55abe2ddc8de6c92a5d9dbbaf Forbid undef passed to new for Moose compat --- diff --git a/Changes b/Changes index 4b0bf08..6e26eb5 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,9 @@ Revision history for Mouse * To improve Moose compat, the third argument to trigger (the attribute metaobject) has been removed (Sartak) + * To improve Moose compat, a single undef passed to new + is now disallowed (Sartak) + 0.19 Sun Mar 8 04:38:01 2009 * Parameterized type constraints for ArrayRef and HashRef (lestrrat) diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 8b3f3a1..dfbeb29 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -143,14 +143,9 @@ sub _generate_BUILDARGS { return <<'...'; do { if ( scalar @_ == 1 ) { - if ( defined $_[0] ) { - ( ref( $_[0] ) eq 'HASH' ) + ( ref( $_[0] ) eq 'HASH' ) || Carp::confess "Single parameters to new() must be a HASH ref"; - +{ %{ $_[0] } }; - } - else { - +{}; - } + +{ %{ $_[0] } }; } else { +{@_}; diff --git a/lib/Mouse/Object.pm b/lib/Mouse/Object.pm index 2a80b0e..bac4fa0 100644 --- a/lib/Mouse/Object.pm +++ b/lib/Mouse/Object.pm @@ -68,13 +68,9 @@ sub BUILDARGS { my $class = shift; if (scalar @_ == 1) { - if (defined $_[0]) { - (ref($_[0]) eq 'HASH') - || confess "Single parameters to new() must be a HASH ref"; - return {%{$_[0]}}; - } else { - return {}; - } + (ref($_[0]) eq 'HASH') + || confess "Single parameters to new() must be a HASH ref"; + return {%{$_[0]}}; } else { return {@_}; diff --git a/t/029-new.t b/t/029-new.t index d387bdf..4e642eb 100644 --- a/t/029-new.t +++ b/t/029-new.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 7; use Test::Exception; do { @@ -28,7 +28,16 @@ throws_ok { Class->new('non-hashref scalar'); } qr/Single parameters to new\(\) must be a HASH ref/; -lives_ok { +throws_ok { Class->new(undef); -} "Class->new(undef) specifically doesn't throw an error. weird" +} qr/Single parameters to new\(\) must be a HASH ref/; + +Class->meta->make_immutable; + +throws_ok { + Class->new('non-hashref scalar'); +} qr/Single parameters to new\(\) must be a HASH ref/; +throws_ok { + Class->new(undef); +} qr/Single parameters to new\(\) must be a HASH ref/;