SmartMatch::Sugar - Smart match friendly tests.
use SmartMatch::Sugar; if ( $data ~~ non_empty_array ) { @$data; } if ( $object ~~ inv_isa("Class") { }
This module provides simple sugary tests that work on the right hand side of a smart match.
All exports are managed by Sub::Exporter so they can be renamed, aliased, etc.
I suggest using namespace::clean to remove these subroutines from your namespace.
namespace::clean
Returns true for any value except code references (this doesn't work because smart match will check for reference equality instead of evaluating).
Returns false for any value
Returns true if the value is an object with overloads. Doesn't return true for class names which have overloads.
Note that putting an overloaded object in a smart match will cause an error unless fallback is true or the object overloads ~~, in which case the matcher sub will not get a chance to work anyway.
fallback
~~
Returns true if the value is an object with string overloading..
Returns true if the value is blessed.
Returns true if Class::Inspector thinks the value is a loaded class.
Returns true if $object->isa($class). Also works on classes.
$object->isa($class)
The reason this check is not called just isa is because if you import that into an OO class then your object's isa method is now bogus.
isa
inv stands for invocant, it's the least sucky name I could muster.
inv
Returns true if $object->can($method).
$object->can($method)
Like inv_isa, also returns true for classes that can $method.
inv_isa
$method
Returns true if $object->DOES($role). Also works for classes.
$object->DOES($role)
Returns true if the item is not a ref, but is defined. Similar to non_empty_string but doesn't involve checking the length, or truth.
non_empty_string
Checks that a value is defined, not a reference, and has a non zero string length.
Check that the string's length is equal to $length.
Check that the value is a non blessed array.
Check that the value is an array with at least one element.
Will not accept objects.
Check that the value is an array and that scalar(@$array) == $length.
scalar(@$array) == $length
Check that the array is even sized (can be assigned to a hash).
Check that the value is a non blessed hash.
Check that the value is a hash with some entries.
Check that the value is a hash with $size entries in it.
$size
Will match the value against the block. Unlike a raw subroutine, this will not distribute over arrays and hashes.
This module is maintained using Darcs. You can get the latest version from http://nothingmuch.woobling.org/code, and use darcs send to commit changes.
darcs send
Yuval Kogman <nothingmuch@woobling.org>
Copyright (c) 2008 Yuval Kogman. All rights reserved This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install SmartMatch::Sugar, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SmartMatch::Sugar
CPAN shell
perl -MCPAN -e shell install SmartMatch::Sugar
For more information on module installation, please visit the detailed CPAN module installation guide.