Sub::PatMat

Sub::PatMat can call a version of subroutine depending on its arguments.
Download

Sub::PatMat Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Anton Berezin
  • Publisher web site:
  • http://search.cpan.org/~gruber/Net-DNS-ZoneCheck-0.01/ZoneCheck.pm

Sub::PatMat Tags


Sub::PatMat Description

Sub::PatMat can call a version of subroutine depending on its arguments. Sub::PatMat can call a version of subroutine depending on its arguments.SYNOPSIS use Sub::PatMat; # basics: sub fact : when($_ $b) { 1 } print join ", ", sort mysort (3,1,2); # intuiting parameter names: sub dispatch : when($ev eq "help") { my ($ev) = @_; print "helpn" } sub dispatch : when($ev eq "blah") { my ($ev) = @_; print "blahn" } dispatch("help"); dispatch("blah"); # no fallback, this will die: dispatch("hest"); # dies with "Bad match" # silly sub do_something : when(full_moon()) { do_one_thing() } sub do_something { do_something_else() }The Sub::PatMat module provides the programmer with the ability to define a subroutine multiple times and to specify what version of the subroutine should be called, depending on the parameters passed to it (or any other condition).This is somewhat similar to argument pattern matching facility provided by many programming languages.To use argument pattern matching on a sub, the programmer has to specify the when attribute. The parameter to the attribute must be a single Perl expression.When the sub is called, those expressions are evaluated consequitively until one of them evaluates to a true value. When this happens, the corresponding version of a sub is called.If none of the expressions evaluates to a true value, a Bad Match exception is thrown.It is possible to specify a fall-back version of the function by doing one of the following:specifying when without an expression specifying when with an empty expression not specifying the when attribute at allPlease note that it does not make sense to specify any non-fall-back version of the sub after the fall-back version, since such will never be called.There is an additional limitation for the last form of the fall-back version (the one without the when attribute at all), namely, it must be the last version of the sub defined.It is possible to specify named sub parameters in the when-expression. This facility is highly experimental and is currently limited to scalar parameters only. The named sub parameters are extracted from expressions of the form my (parameter list) = @_;anywhere in the body of the sub.Limitations:· The ability to intuit parameter names is very limited and without doubts buggy.· The when attribute condition is limited to a single Perl expression.What's New in This Release:· Perl


Sub::PatMat Related Software