Class::Param::Callback - Param instance with callbacks


Class-Param documentation Contained in the Class-Param distribution.

Index


Code Index:

NAME

Top

Class::Param::Callback - Param instance with callbacks

SYNOPSIS

Top

    %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] }  }
    );

DESCRIPTION

Top

Construct a params instance using callbacks.

METHODS

Top

new

This method takes a hash of parameters. The following options are valid:

get
    get => sub {
        my ( $self, $name ) = @_;
        return $hash{ $name };
    }

Required.

set
    set => sub {
        my ( $self, $name, $value ) = @_;
        return $hash{ $name } = $value;
    }

Required.

names
    names => sub {
        my ( $self ) = @_;
        return keys %hash;
    }

Required.

remove
    remove => sub {
        my ( $self, $name ) = @_;
        return delete $hash{ $name };
    }

Required.

clear
    clear => sub {
        my ( $self ) = @_;
        return %hash = ();
    }

Optional.

count
    count => sub {
        my ( $self ) = @_;
        return scalar keys %hash;
    }

Optional.

has
    has => sub {
        my ( $self, $name ) = @_;
        return exists $hash{ $name };
    }

Optional.

param
    param => sub { }

Optional. See Class::Param::Base for expected behavior.

add
    add => sub { }

Optional. See Class::Param::Base for expected behavior.

scan
    scan => sub { }

Optional. See Class::Param::Base for expected behavior.

as_hash
    param => sub { }

Optional. See Class::Param::Base for expected behavior.

SEE ASLO

Top

Class::Param.

AUTHOR

Top

Christian Hansen chansen@cpan.org

COPYRIGHT

Top


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__