| Hash-Param documentation | Contained in the Hash-Param distribution. |
Hash::Param - CGI/Catalyst::Request-like parameter-hash accessor/mutator
Version 0.04
my $params = Hash::Param->new(parameters => {
qw/a 1 b 2 c 3/,
d => [qw/4 5 6 7/],
})
$result = $params->param( a ) # Returns 1
$result = $params->param( d ) # Returns 4
@result = $params->param( d ) # Returns 4, 5, 6, 7
@result = $params->params # Returns a, b, c, d
$result = $params->params # Returns { a => , b => 2,
c => 3, d => [ 4, 5, 6, 7 ] }
@result = $params->params( a, b, d ) # Returns 1, 2, [ 4, 5, 6, 7 ]
%result = $params->slice( a, b ) # Returns a => 1, b => 2
$params->param( a => 8 ) # Sets a to 8
$params->param( a => 8, 9 ) # Sets a to [ 8, 9 ]
Hash::Param provides a CGI-param-like accessor/mutator for a hash
Returns a new Hash::Param object with the given parameters
<params> should be a HASH reference (the object will be initialized with an empty hash if none is given)
<is> should be either ro or rw to indicate where the object is read-only or read-write, respectively
The object will be read-write by default
Returns the value of <param>
If the <param> value is an ARRAY reference:
Sets the value of <param> to <value>
Throws an error if $params is read-only
Sets the value of <param> to an ARRAY reference consisting of [ <value>, <value>, ... ]
Throws an error if $params is read-only
Returns a list of every param name
An alias for ->param
Returns a list containing with value of each <param>
Returns an ARRAY reference in scalar context
If $params is read-only, then each ARRAY reference value will be copied first (if any)
Returns a hash of the parameters stored in $param
In scalar context, will return a HASH reference (which will be copied first if $params is read-only)
Sets the parameters of $params via <hash> (which should be a HASH reference)
Throws an error if $params is read-only
An alias for ->params
Sets the parameters of $params via <hash> (which should be a HASH reference)
Throws an error if $params is read-only
Returns the value of <param>
Does the same as $param->param( <param> )
Returns a list containing with value of each <param>
Does the same as $param->params( <param>, <param>, ... )
Returns a hash of the parameters stored in $param
Does the same as $param->params
Returns a hash slice of <param>, <param>, ...
Returns a HASH reference in scalar context
If $params is read-only, then the slice will be cloned
Robert Krimen, <rkrimen at cpan.org>
You can contribute or fork this project via GitHub:
http://github.com/robertkrimen/hash-param/tree/master
git clone git://github.com/robertkrimen/hash-param.git Hash-Param
Please report any bugs or feature requests to bug-hash-param at rt.cpan.org, or through
the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-Param. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Hash::Param
You can also look for information at:
Copyright 2008 Robert Krimen, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Hash-Param documentation | Contained in the Hash-Param distribution. |
package Hash::Param; use warnings; use strict;
our $VERSION = '0.04'; use Moose; use Carp::Clan; use Hash::Slice; has parameters => qw/accessor _parameters isa HashRef lazy_build 1/; sub _build_parameters { return {}; } has _is_rw => qw/is rw default 1/; sub BUILD { my $self = shift; my $given = shift; if (my $is = $given->{is}) { if ($is =~ m/^(?:rw|readwrite|writable)$/i) { $self->_is_rw(1); } elsif ($is =~ m/^(?:ro|readonly)$/i) { $self->_is_rw(0); } else { croak "Don't understand this read/write designation: \"$is\""; } } for (qw/params hash data from/) { last if $self->{_parameters}; $self->_parameters($given->{$_}) if $given->{$_}; } }
sub parameter { my $self = shift; return $self->param(@_); } sub param { my $self = shift; if (@_ == 0) { return keys %{ $self->_parameters }; } if (@_ == 1) { my $param = shift; if (ref $param eq "ARRAY") { return $self->params(@$param); } unless (exists $self->_parameters->{$param}) { return wantarray ? () : undef; } if (ref $self->_parameters->{$param} eq 'ARRAY') { return (wantarray) ? @{ $self->_parameters->{$param} } : $self->_parameters->{$param}->[0]; } else { return (wantarray) ? ($self->_parameters->{$param}) : $self->_parameters->{$param}; } } elsif (@_ > 1) { my $field = shift; croak "Unable to modify readonly parameter \"@{[ $field || '' ]}\"" unless $self->_is_rw; $self->_parameters->{$field} = @_ > 1 ? [ @_ ] : $_[0]; } }
sub parameters { my $self = shift; return $self->params(@_); } sub params { my $self = shift; if (@_) { if (1 == @_ && ref $_[0] eq "HASH") { croak "Unable to modify readonly parameters" unless $self->_is_rw; $self->_parameters($_[0]); } else { my @params = map { $self->_parameters->{$_} } @_; @params = map { ref $_ eq "ARRAY" ? [ @$_ ] : $_ } @params unless $self->_is_rw; return wantarray ? @params : \@params; } } else { return wantarray ? keys %{ $self->_parameters } : $self->_is_rw ? $self->_parameters : { %{ $self->_parameters } }; } }
sub data { my $self = shift; $self->params(shift); }
sub get { my $self = shift; return $self->params unless @_; return $self->params(@_) if @_ > 1; return $self->param(@_); }
sub slice { my $self = shift; my $parameters = $self->_parameters; return $self->_is_rw ? Hash::Slice::slice $parameters, @_ : Hash::Slice::clone_slice $parameters, @_; } use MooseX::MakeImmutable; MooseX::MakeImmutable->lock_down;
1; # End of Hash::Param