From: Yuval Kogman Date: Sun, 27 Jan 2008 00:13:40 +0000 (+0000) Subject: init_arg can be undef X-Git-Tag: 0_53~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2e877f58520b9c3a3c1cbb403c7f2be4c543a9c7;p=gitmo%2FClass-MOP.git init_arg can be undef --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 720a9b5..06586ce 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -135,9 +135,7 @@ Class::MOP::Package->meta->add_attribute( # rather than re-produce it here 'namespace' => \&Class::MOP::Package::namespace }, - # NOTE: - # protect this from silliness - init_arg => '!............( DO NOT DO THIS )............!', + init_arg => undef, default => sub { \undef } )) ); @@ -172,9 +170,7 @@ Class::MOP::Module->meta->add_attribute( # rather than re-produce it here 'version' => \&Class::MOP::Module::version }, - # NOTE: - # protect this from silliness - init_arg => '!............( DO NOT DO THIS )............!', + init_arg => undef, default => sub { \undef } )) ); @@ -193,9 +189,7 @@ Class::MOP::Module->meta->add_attribute( # rather than re-produce it here 'authority' => \&Class::MOP::Module::authority }, - # NOTE: - # protect this from silliness - init_arg => '!............( DO NOT DO THIS )............!', + init_arg => undef, default => sub { \undef } )) ); @@ -240,9 +234,7 @@ Class::MOP::Class->meta->add_attribute( # rather than re-produce it here 'superclasses' => \&Class::MOP::Class::superclasses }, - # NOTE: - # protect this from silliness - init_arg => '!............( DO NOT DO THIS )............!', + init_arg => undef, default => sub { \undef } )) ); diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index 55b1518..f714c5b 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -49,10 +49,16 @@ sub new { "wrap then in a CODE reference (ex: sub { [] } and not [])") if exists $options{default} && ref $options{default}; } + if( $options{required} and not( defined($options{builder}) || defined($options{init_arg}) || exists $options{default} ) ) { + confess("A required attribute must have either 'init_arg', 'builder', or 'default'"); + } bless { '$!name' => $name, '$!accessor' => $options{accessor}, '$!reader' => $options{reader}, + # NOTE: + # protect this from silliness + init_arg => '!............( DO NOT DO THIS )............!', '$!writer' => $options{writer}, '$!predicate' => $options{predicate}, '$!clearer' => $options{clearer}, @@ -88,7 +94,7 @@ sub initialize_instance_slot { # if nothing was in the %params, we can use the # attribute's default value (if it has one) - if(exists $params->{$init_arg}){ + if(defined $init_arg and exists $params->{$init_arg}){ $meta_instance->set_slot_value($instance, $self->name, $params->{$init_arg}); } elsif (defined $self->{'$!default'}) {