Class-MethodMaker
view release on metacpan or search on metacpan
components/array.m view on Meta::CPAN
join(', ', @bad_opt), "\n");
}
my $type = $options->{type};
croak "argument to -type ($type) must be a simple value\n"
unless ! ref $type;
my $forward = $options->{forward};
my @forward;
if ( defined $forward ) {
if ( ref $forward ) {
croak("-forward option can only handle arrayrefs or simple values " .
"($forward)\n")
unless UNIVERSAL::isa($forward, 'ARRAY');
@forward = @$forward;
print "Value '$_' passed to -forward is not a simple value"
for grep ref($_), @forward;
} else {
@forward = $forward;
}
}
my ($default, $dctor, $default_defined);
if ( exists $options->{default} ) {
croak("Cannot specify both default & default_ctor options to array ",
"(attribute $name\n")
if exists $options->{default_ctor};
$default = $options->{default};
$default_defined = 1;
} elsif ( exists $options->{default_ctor} ) {
if ( ! ref $options->{default_ctor} ) {
my $meth = $options->{default_ctor};
croak("default_ctor can only be a simple value when -type is in effect",
" (attribute $name)\n")
unless defined $type;
croak("default_ctor must be a valid identifier (or a code ref): $meth ",
"(attribute $name)\n")
unless $meth =~ /^[A-Za-z_][A-Za-z0-9_]*/;
$dctor = sub { $type->$meth(@_) };
} else {
$dctor = $options->{default_ctor};
croak("Argument to default_ctor must be a simple value or a code ref ",
" (attribute $name)\n")
if ! UNIVERSAL::isa($dctor, 'CODE');
}
$default_defined = 1;
}
my ($tie_class, @tie_args);
if ( exists $options->{tie_class} ) {
$tie_class = $options->{tie_class};
if ( exists $options->{tie_args} ) {
my $tie_args = $options->{tie_args};
@tie_args = ref $tie_args ? @$tie_args : $tie_args;
}
} elsif ( exists $options->{tie_args} ) {
carp "tie_args option ignored in absence of tie_class(attribute $name)\n";
}
# callback options
my @read_callbacks = ref $options->{read_cb} eq 'ARRAY' ?
@{$options->{read_cb}} :
$options->{read_cb}
if exists $options->{read_cb};
my @store_callbacks = ref $options->{store_cb} eq 'ARRAY' ?
@{$options->{store_cb}} :
$options->{store_cb}
if exists $options->{store_cb};
%%STORDECL%%
# Predefine keys for subs we always want to exist (because they're
# referenced by other subs)
my %names = map {; $_ => undef } qw( * *_reset *_index );
return {
=pod
Methods available are:
=head3 C<*>
I<Created by default.> This method returns the list of values stored in the
slot. If any arguments are provided to this method, they B<replace> the
current list contents. In an array context it returns the values as an array
and in a scalar context as a reference to an array. Note that this reference
is no longer a direct reference to the storage, in contrast to
Class::MethodMaker v1. This is to protect encapsulation. See x_ref if you
need that functionality (and are prepared to take the associated risk.) This
function no longer auto-expands arrayrefs input as arguments, since that makes
it awkward to set individual values to arrayrefs. See x_setref for that
functionality.
If a default value is in force, then that value will be auto-vivified (and
therefore set) for each otherwise I<unset> (not I<not defined>) value up to
the array max (so new items will not be appended)
=cut
'*' =>
sub : method {
my $z = \@_; # work around stack problems
my $want = wantarray;
print STDERR "W: ", $want, ':', join(',',@_),"\n"
if DEBUG;
# We also deliberately avoid instantiating storage if not
# necessary.
if ( @_ == 1 ) {
%%DEFAULT_ON%%
if ( exists %%STORAGE%% ) {
for (0..$#{%%STORAGE%%}) {
%%DEFCHECK@(%%STORAGE%%->[$_])%%;
}
}
%%DEFAULT_OFF%%
if ( exists %%STORAGE%% ) {
if ( ! defined $want ) {
return;
} elsif ( $want ) {
return @{%%STORAGE%%};
} else {
( run in 0.728 second using v1.01-cache-2.11-cpan-140bd7fdf52 )