Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EvanCarroll/645659 to your computer and use it in GitHub Desktop.
Save EvanCarroll/645659 to your computer and use it in GitHub Desktop.
This file demonstrates the counter-intuitive behavior of Moose in regards to strict-defaults, and constructor provided arguments.
package Class;
use Moose;
has "foo" => (
isa => 'Str'
, is => 'ro'
, predicate => '_has_foo'
);
has "bar" => (
is => 'ro'
, isa => 'Str'
, default => sub {
my $self = shift;
die "E100 Attribute foo was not set with implicit Moose-provided constructor, before attribute bar's default"
unless $self->_has_foo
;
"DOOG";
}
);
package main;
use Test::More tests => 2;
my $o;
eval {
$o = Class->new({foo=>"GOOD"})->bar
};
SKIP: {
unlike ( $@, qr/E100/, "constructor provided arguments filled before non-lazy defaults" )
|| skip "we've got an error", 1
;
like ( $o->bar, qr/DOOG/, "non-lazy default dependant on constructor provided argument set properly" );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment