use Carp 'confess';
use Scalar::Util 'blessed', 'reftype', 'weaken';
-our $VERSION = '0.20';
+our $VERSION = '0.23';
our $AUTHORITY = 'cpan:STEVAN';
use base 'Class::MOP::Object';
"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},
# 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'}) {
=item I<predicate>
-This is a basic test to see if the value of the attribute is not
-C<undef>. It will return true (C<1>) if the attribute's value is
-defined, and false (C<0>) otherwise.
+This is a basic test to see if any value has been set for the
+attribute. It will return true (C<1>) if the attribute has been set
+to any value (even C<undef>), and false (C<0>) otherwise.
+
+B<NOTE:>
+The predicate will return true even when you set an attribute's
+value to C<undef>. This behaviour has changed as of version 0.43. In
+older versions, the predicate (erroneously) checked for attribute
+value definedness, instead of presence as it is now.
+
+If you really want to get rid of the value, you have to define and
+use a I<clearer> (see below).
+
=item I<clearer>
=item B<has_value ($instance)>
-Returns a boolean indicating if the item in the C<$instance> has a value in it.
+Return a boolean indicating if the item in the C<$instance> has a value in it.
This is basically what the default C<predicate> method calls.
=item B<clear_value ($instance)>
=item B<default (?$instance)>
-As noted in the documentation for C<new> above, if the I<default>
-value is a CODE reference, this accessor will pass a single additional
-argument C<$instance> into it and return the value.
+Return the default value for the attribute.
+
+If you pass in an C<$instance> argument to this accessor and the
+I<default> is a CODE reference, then the CODE reference will be
+executed with the C<$instance> as its argument.
=item B<slots>
-Returns a list of slots required by the attribute. This is usually
+Return a list of slots required by the attribute. This is usually
just one, which is the name of the attribute.
=item B<get_read_method>
=head1 COPYRIGHT AND LICENSE
-Copyright 2006, 2007 by Infinity Interactive, Inc.
+Copyright 2006-2008 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>