NAME
Devel::TakeHashArgs - make a hash from @_ and set defaults in subs while checking that all mandatory arguments are present
SYNOPSIS
use Devel::TakeHashArgs;
use Carp;
sub foos {
get_args_as_hash(\@_, \my %args, { foos => 'bars' }, [ qw(ber1 ber2) ] )
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 and checking for mandatory arguments.
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 => 'defaults2!',
},
[ qw(mandatory1 mandatory2) ],
)
or croak $@;
}
The sub makes out a hash out of @_
, checks that all mandatory arguments were provided (if any), assigns optional defaults (if any) and fills the passed hashref. Returns 1
for success and 0
for failure, upon failure the reason for it will be available in $@
variable...
The sub takes two mandatory arguments: the reference to an array (the @_
but it can be any array) and a reference to a hash where you want your args to go. The other two optional arguments are a hashref which would contain the defaults to assign unless the argument is present in the passed array. Following the hashref is an arrayref of mandatory arguments. If you want to specify mandatory arguments without providing any defaults just pass in an empty hashref as a third argument, i.e. get_args_as_hash( \@_, \ my %args, {}, [ qw(mandatory1 mandatory2) ])
Basically the above code is roughly 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;
for ( qw(mandatory1 mandatory2) ) {
exists $args{$_}
or croak "Missing mandatory argument `$_`";
}
%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.
CAVEATS AND LIMITATIONS
All argument names (the hash keys) will be lowercased therefore when setting defaults and mandatory arguments you can only use all lowercase names. On a plus side, user can use whatever case they want :)
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.