Sub::ParamFrame

Supply key alias and defaults of named arguments
Download

Sub::ParamFrame Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Josef Schnbrunner
  • Publisher web site:
  • http://search.cpan.org/~schoejo/Tie-Hash-KeysMask-0.01/lib/Tie/Hash/KeysMask.pm

Sub::ParamFrame Tags


Sub::ParamFrame Description

Supply key alias and defaults of named arguments Sub::ParamFrame is a Perl module to supply key alias and defaults of named arguments.SYNOPSIS use Sub::ParamFrame ':all'; sub myFunc { # Define the rule how named arguments shell be processed pfrule 'D' => , # assign default key-value association. 'M' => ; # keys mask function and fixed arguments # access a certain count of positional arguments here my @PARGS = splice @_,0,N; # Load named arguments. That is create a hash according to arguments # passed to pfrule. This hash also contains default arguments my $arg = pfload @_; # access argument or default of name NAME do_something_with $arg->{NAME}; ...... # or likewise access key NAME, in addition erase it from $arg do_something_with delete $arg->{NAME}; ....... # could also be used in list context accessing multiple arguments .. ($v1,$v2,...) = delete @$arg{NAME1,NAME2,...}; .... } ##myFuncA couple of modules already deal with named parameters and default values, see Sub::Parameters,Sub::NamedParams,Sub::Declaration,Perl6::Parameters.This solution pursues another scope of usability, covers distinct features and uses another syntactic approach.Named parameters are identified by a hash built from the argument vector @_. The generation and behavior of this hash will be controlled by a rule. If one subroutine myFunc() uses such a rule, this rule appears as a command within the subroutine's body. The rule lays down one ore both of two properties:1. a name-value-association of defaults and 2. an alias mapping for argument names.Once this rule is passed by call of pfrule from the first invocation of myFunc, the generation of the hash by the pfload function shell follow this rule at once and during future calls.pfrule appears before pfload and will be executed only once, only when the calling subroutine runs first time.Two named optional arguments are defined for pfrule, neither must be present.Arguments of pfrule 'D' => Defines a default key-value-association. pfload stores this association before arguments usually from @_ advance and may override some default values. 'M' => $mask where $mask = sub {...} 'M' => or = &fmaskKeys mask function and optionally fixed arguments. If 'M' is omitted pfload shall return just a hash. If 'M' is present it causes pfload to return a hash tied to class Tie::Hash::KeysMask such that each access to the hash triggers a key translation: $k => $mask->($k,P(1),...P(m))Instead of a CODE 'M'=>$mask could take one of the following particular values 'M' => 'lc' or 'M' => 'uc' or 'M' => \%T which will be translated into a CODE as follows 'lc' => sub { lc $_ } # omit case of character distinction 'uc' => sub { uc $_ } # with 'uc' or 'lc' translations \%T => sub { exists $T{$_} ? $T{$_} : $_} # hash %T defines aliasesContrary to other approaches to named arguments, one may choose freely the position of the first named argument within @_. Any amount of @_ may be shifted onto positional parameters before the command pfload @_ takes the remaining pairs of key=>value. Of course arguments different from @_ are also allowed behind pfload. Requirements: · Perl


Sub::ParamFrame Related Software