| Class-Param documentation | Contained in the Class-Param distribution. |
Class::Param::Callback - Param instance with callbacks
%store = ();
$param = Class::Param::Callback->new(
get => sub { return $store{ $_[1] } },
set => sub { return $store{ $_[1] } = $_[2] },
has => sub { return exists $store{ $_[1] } },
names => sub { return keys %store },
remove => sub { return delete $store{ $_[1] } }
);
Construct a params instance using callbacks.
This method takes a hash of parameters. The following options are valid:
get => sub {
my ( $self, $name ) = @_;
return $hash{ $name };
}
Required.
set => sub {
my ( $self, $name, $value ) = @_;
return $hash{ $name } = $value;
}
Required.
names => sub {
my ( $self ) = @_;
return keys %hash;
}
Required.
remove => sub {
my ( $self, $name ) = @_;
return delete $hash{ $name };
}
Required.
clear => sub {
my ( $self ) = @_;
return %hash = ();
}
Optional.
count => sub {
my ( $self ) = @_;
return scalar keys %hash;
}
Optional.
has => sub {
my ( $self, $name ) = @_;
return exists $hash{ $name };
}
Optional.
param => sub { }
Optional. See Class::Param::Base for expected behavior.
add => sub { }
Optional. See Class::Param::Base for expected behavior.
scan => sub { }
Optional. See Class::Param::Base for expected behavior.
param => sub { }
Optional. See Class::Param::Base for expected behavior.
Christian Hansen chansen@cpan.org
This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
| Class-Param documentation | Contained in the Class-Param distribution. |
package Class::Param::Callback; use strict; use warnings; use base 'Class::Param::Base'; use Params::Validate qw[]; BEGIN { my @required = qw[ get set names remove ]; my @optional = qw[ add clear count has param scan as_hash ]; my $spec = { }; foreach my $method ( @required, @optional ) { no strict 'refs'; *$method = sub { my $self = shift; my $code = $self->{$method} || Class::Param::Base->can($method); return $self->$code(@_); }; } foreach my $required ( @required ) { $spec->{ $required } = { type => Params::Validate::CODEREF, optional => 0 }; } foreach my $optional ( @optional ) { $spec->{ $optional } = { type => Params::Validate::CODEREF, optional => 1 }; } *new = sub { my $class = ref $_[0] ? ref shift : shift; my $self = Params::Validate::validate_with( params => \@_, spec => $spec, called => "$class\::new" ); return bless( $self, $class ); }; } 1; __END__