NAME
Hash::Args - Coerces argument lists into HASH references for convenience
SYNOPSIS
use Hash::Args;
# ARRAY reference
my $ref = hash([ foo => 'bar', baz => 'qux' ]);
# HASH reference
my $ref = hash({ foo => 'bar', baz => 'qux' });
# LIST of key/value pairs
my $ref = hash( foo => 'bar', baz => 'qux' );
# in a sub-routine
sub method {
my ( $self, $param, $args ) = ( shift, shift, hash( @_ ) );
...
}
# ... or ...
sub method {
my $self = shift;
my $param = shift;
my $args = hash( @_ );
}
# ... or ...
sub method {
my ( $self, $param, @args ) = @_;
my $args = hash( @args );
...
}
DESCRIPTION
The primary purpose of Hash::Args is to provide an easy way to coerce a list of values into a HASH
reference. It does this in one of a few ways. It can accept a plain LIST
of key/value pairs, a HASH
reference or an ARRAY
reference of key/value pairs.
EXPORTS
hash( \@ARRAY | \%HASH )
hash( LIST )
This sub-routine transforms its arguments into a HASH
reference. It does this by first inspecting @_
and then running in one of two modes of operation. If there is only one argument and that argument is a reference, the first mode of operation is selected; otherwise the second is selected.
In the first mode of operation a check is made to see what type of reference was passed in. If it is an ARRAY
reference, an exception is thrown if its length is odd. Otherwise the array is assumed to contain key/value pairs and is coerced into a HASH
reference as such. If the reference passed in is a HASH
reference or appears to be an object, it is simply returned as-is. If the reference passed in is none of these an exception is thrown.
In the second mode of operation the LIST
that was passed in is transformed into a HASH
reference by treating the list as key/value pairs. If the list contains an odd number of elements, an exception is thrown.
AUTHOR
jason hord <pravus@cpan.org>
COPYRIGHT
Copyright (c) 2012-2014, jason hord
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.