NAME
Devel::TakeHashArgs - make a hash from @_ and set defaults in subs
SYNOPSIS
use Devel::TakeHashArgs;
use Carp;
sub foos {
get_args_as_hash(\@_, \my %args, { foos => 'bars' } )
or croak $@;
print map { "$_ => $args{$_}\n" } keys %args;
}
DESCRIPTION
The module is a short utility I made after being sick and tired of writing redundant code to make a hash out of args when they are passed as key/value pairs including setting their defaults.
EXPORT
The module has only one sub and it's exported by default.
get_args_as_hash
sub foos {
get_args_as_hash( \@_, \my %args, { some => 'defaults', more => 'defaults!' } )
or croak $@;
}
The sub makes out a hash out of @_
, assigns optional defaults and fills the passed hashref. Returns 1
for success and 0
for failure, upon failure the reason for it will be available in $@
variable... actually, the only reason it would fail is if @_
would contain uneven number of arguments.
The sub takes two mandatory arguments: the reference to an array (the @_
but it can be any array), a reference to a hash where you want your args to go, and third optional hashref argument which would contain the defaults to assign unless the argument is present in the passed array.
Basically the above code is the same as:
sub foos {
croak "Must have even number of arguments to new()"
if @_ & 1;
my %args = @_;
$args{ +lc } = delete $args{ $_ } for keys %args;
%args = (
some => 'defaults',
more => 'defaults!',
%args,
);
);
It's not much but you get pretty sick and tired after you type (copy/paste) that bit over 150 times.
AUTHOR
Zoffix Znet, <zoffix at cpan.org>
(http://zoffix.com, http://haslayout.net)
BUGS
Please report any bugs or feature requests to bug-devel-takehashargs at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-TakeHashArgs. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Devel::TakeHashArgs
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
COPYRIGHT & LICENSE
Copyright 2008 Zoffix Znet, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.