match::simple::sugar - a few extras for match::simple
This module provides a given/when substitute for match::simple.
given
when
use match::simple::sugar; for ( $var ) { when 'foo', then { ... }; when 'bar', 'baz', then { ... }; ...; # otherwise }
It also provides a function for numeric matching (because match::simple always assumes you want stringy matching if the right-hand-side is a defined non-reference value).
use match::simple::sugar; for ( $var ) { when numeric 0, then { ... }; when numeric 1, then { ... }; ...; # otherwise }
This module exports three functions when, then, and numeric, and also re-exports match from match::simple.
then
numeric
match
The when and then functions are intended to be used together, inside a for ( SCALAR ) { ... } block. The block acts as a topicalizer (it sets $_) and also a control-flow mechanism (when can use next to jump out of it). Any other use of when and then is unsupported.
for ( SCALAR ) { ... }
$_
next
when( @values, $then )
The when function accepts a list of values, followed by a special $then argument.
$then
If $_ matches (according to the definition in match::simple) any of the values, then the $then argument will be executed, and when will use the Perl built-in next keyword to jump out of the surrounding for block.
for
then { ... }
The then function takes a block of code and returns an object suitable for use as when's $then argument.
In the current implementation, the block of code should not inspect @_ or wantarray, and should not use the return, next, last, or redo keywords. (If you set any of the PERL_STRICT, EXTENDED_TESTING, AUTHOR_TESTING, or RELEASE_TESTING environment variables to true, then match::simple::sugar will try to enforce this! This is intended to catch faulty then blocks when running your test suite.)
@_
wantarray
return
last
redo
PERL_STRICT
EXTENDED_TESTING
AUTHOR_TESTING
RELEASE_TESTING
The numeric function accepts a number and returns a blessed object which has a MATCH method. The MATCH method returns true if it is called with a single defined non-referece scalar that is numerically equal to the original number passed to numeric. Example:
MATCH
numeric( '5.0' )->MATCH( '5.000' ); # true
This is intended for use in cases like:
if ( match $var, numeric 1 ) { ...; }
Please report any bugs to https://github.com/tobyink/p5-match-simple/issues.
match::simple.
This module uses Exporter::Tiny.
Toby Inkster <tobyink@cpan.org>.
This module is inspired by a talk I gave to Boston.PM.
This software is copyright (c) 2023 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install match::simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm match::simple
CPAN shell
perl -MCPAN -e shell install match::simple
For more information on module installation, please visit the detailed CPAN module installation guide.