X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FAttribute.pm;h=d4f822f185e4a5d8e7b520aec0c322a897117023;hb=7f974e03923c7e2f0914145b3abba575d3ab00af;hp=8650818b77c1e954f383762ea7952819b9415506;hpb=9ca19585ca1a0487cc5bb11e62fc6db1e460c4aa;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index 8650818..d4f822f 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -7,7 +7,8 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed', 'reftype', 'weaken'; -our $VERSION = '0.11'; +our $VERSION = '0.12'; +our $AUTHORITY = 'cpan:STEVAN'; sub meta { require Class::MOP::Class; @@ -33,6 +34,11 @@ sub new { $options{init_arg} = $name if not exists $options{init_arg}; + (is_default_a_coderef(\%options)) + || confess("References are not allowed as default values, you must ". + "wrap then in a CODE reference (ex: sub { [] } and not [])") + if exists $options{default} && ref $options{default}; + bless { name => $name, accessor => $options{accessor}, @@ -102,7 +108,7 @@ sub init_arg { $_[0]->{init_arg} } # (all methods below here are kept intact) sub is_default_a_coderef { - (reftype($_[0]->{default}) && reftype($_[0]->{default}) eq 'CODE') + ('CODE' eq (reftype($_[0]->{default}) || '')) } sub default { @@ -138,7 +144,7 @@ sub detach_from_class { ## Slot management sub set_value { - my ( $self, $instance, $value ) = @_; + my ($self, $instance, $value) = @_; Class::MOP::Class->initialize(Scalar::Util::blessed($instance)) ->get_meta_instance @@ -146,11 +152,11 @@ sub set_value { } sub get_value { - my ( $self, $instance ) = @_; + my ($self, $instance) = @_; Class::MOP::Class->initialize(Scalar::Util::blessed($instance)) ->get_meta_instance - ->get_slot_value( $instance, $self->name ); + ->get_slot_value($instance, $self->name); } ## Method generation helpers @@ -158,8 +164,8 @@ sub get_value { sub generate_accessor_method { my $attr = shift; return sub { - $attr->set_value( $_[0], $_[1] ) if scalar(@_) == 2; - $attr->get_value( $_[0] ); + $attr->set_value($_[0], $_[1]) if scalar(@_) == 2; + $attr->get_value($_[0]); }; } @@ -181,7 +187,7 @@ sub generate_reader_method { my $attr = shift; return sub { confess "Cannot assign a value to a read-only accessor" if @_ > 1; - $attr->get_value( $_[0] ); + $attr->get_value($_[0]); }; } @@ -202,7 +208,7 @@ sub generate_reader_method_inline { sub generate_writer_method { my $attr = shift; return sub { - $attr->set_value( $_[0], $_[1] ); + $attr->set_value($_[0], $_[1]); }; }