Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

kwargs(3pm) [debian man page]

Kwargs(3pm)						User Contributed Perl Documentation					       Kwargs(3pm)

NAME
Kwargs - Simple, clean handing of named/keyword arguments. VERSION
version 0.01 SYNOPSIS
use Kwargs; # just named my ($foo, $bar, baz) = kw @_, qw(foo bar baz); # positional followed by named my ($pos, $opt_one, $opt_two) = kwn @_, 1, qw(opt_one opt_two) # just a hashref my $opts = kw @_; # positional then hashref my ($one, $two, $opts) = kwn @_, 2; WHY
? Named arguments are good, especially when you take lots of (sometimes optional) arguments. There are two styles of passing named arguments (by convention) in perl though, with and without braces: sub foo { my $args = shift; my $bar = $args->{bar}; } foo({ bar => 'baz' }); sub bar { my %args = @_; my $foo = $args{foo}; } bar(foo => 'baz'); If you want to support both calling styles (because it should be mainly a style issue), then you have to do something like this: sub foo { my $args = ref $_[0] eq 'HASH' ? $_[0] : { @_ }; my $bar = $args->{bar}; } Which is annoying, and not even entirely correct. What if someone wanted to pass in a tied object for their optional arguments? That could work, but what are the right semantics for checking for it? It also gets uglier if you want to unpack your keyword arguments in one line for clarity: sub foo { my ($one, $two, $three) = @{ ref $_[0] eq 'HASH' ? $_[0] : { @_ } }{qw(one two three) }; } Did I say clarity? HAHAHAHAHA! Surely no one would actually put something like that in his code. Except I found myself typing this very thing, and That Is Why. EXPORTS
Two functions (kw and kwn) are exported by default. You can also ask for them individually or rename them to something else. See Sub::Exporter for details. kw(@array, @names) Short for "kwn(@array, 0, @names)" kwn(@array, $number_of_positional_args, @names) Conceptually shifts off n positional arguments from array, then figures out whether the rest of the array is a list of key-value pairs or a single argument (usually, but not necessarily, a hashref). If you passed in any @names, these are used as keys into the hash, and the values at those keys are appended to any positional arguments and returned. If you do not pass @names, you will get a hashref (or whatever the single argument was, like a tied object) back. Note that if the single argument cannot be dereferenced as a hashref, this can die. No attempt is made by this module to handle the exception. AUTHOR
Paul Driver <frodwith@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Paul Driver <frodwith@cpan.org>. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.12.4 2011-01-24 Kwargs(3pm)

Check Out this Related Man Page

Attribute::Params::Validate(3)				User Contributed Perl Documentation			    Attribute::Params::Validate(3)

NAME
Attribute::Params::Validate - Validate method/function parameters using attributes SYNOPSIS
use Attribute::Params::Validate qw(:all); # takes named params (hash or hashref) # foo is mandatory, bar is optional sub foo : Validate( foo => 1, bar => 0 ) { ... } # takes positional params # first two are mandatory, third is optional sub bar : ValidatePos( 1, 1, 0 ) { ... } # for some reason Perl insists that the entire attribute be on one line sub foo2 : Validate( foo => { type => ARRAYREF }, bar => { can => [ 'print', 'flush', 'frobnicate' ] }, baz => { type => SCALAR, callbacks => { 'numbers only' => sub { shift() =~ /^d+$/ }, 'less than 90' => sub { shift() < 90 } } } ) { ... } # note that this is marked as a method. This is very important! sub baz : Validate( foo => { type => ARRAYREF }, bar => { isa => 'Frobnicator' } ) method { ... } DESCRIPTION
The Attribute::Params::Validate module allows you to validate method or function call parameters just like Params::Validate does. However, this module allows you to specify your validation spec as an attribute, rather than by calling the "validate" routine. Please see Params::Validate for more information on how you can specify what validation is performed. EXPORT This module exports everthing that Params::Validate does except for the "validate" and "validate_pos" subroutines. ATTRIBUTES o Validate This attribute corresponse to the "validate" subroutine in Params::Validate. o ValidatePos This attribute corresponse to the "validate_pos" subroutine in Params::Validate. OO If you are using this module to mark methods for validation, as opposed to subroutines, it is crucial that you mark these methods with the ":method" attribute, as well as the "Validate" or "ValidatePos" attribute. If you do not do this, then the object or class used in the method call will be passed to the validation routines, which is probably not what you want. CAVEATS You must put all the arguments to the "Validate" or "ValidatePos" attribute on a single line, or Perl will complain. SEE ALSO
Params::Validate AUTHOR
Dave Rolsky, <autarch@urth.org> perl v5.12.1 2010-07-05 Attribute::Params::Validate(3)
Man Page