Leon Timmermans > Smart-Match-0.007 > Smart::Match

Download:
Smart-Match-0.007.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.007   Source  

NAME ^

Smart::Match - Smart matching utilities

VERSION ^

version 0.007

SYNOPSIS ^

 given ($foo) {
     say "We've got a positive number" when positive;
     say "We've got an array" when array;
     say "We've got a non-empty string" when string_length(positive);
 }

DESCRIPTION ^

This module provides a number of helper functions for smartmatching. Some are simple functions that directly match the left hand side, such as

 $foo ~~ positive
 $bar ~~ array

Others are higher-order matchers that take one or more matchers as an argument, such as

 $foo ~~ string_length(positive)
 $bar ~~ any(array, hash)

Do note that ordinary values are matchers too, so

 $baz ~~ any(1,2,3)

will also do what you mean.

FUNCTIONS ^

always()

This always matches.

never()

This never matches.

any(@matchers)

This matches if the left hand side matches any of @matchers.

all(@matchers)

This matches if the left hand side matches all of @matchers.

none(@matchers)

This matches if the left hand side matches none of @matchers.

one(@matchers)

This matches if the left hand side matches exactly one of @matchers.

true()

This matches if the left hand side is true.

false()

This matches if the left hand side is false.

number()

This matches if the left hand side is a number.

integer()

This matches if the left hand side is an integer.

even()

This matches if the left hand side is even.

odd()

This matches if the left hand side is odd.

more_than($cutoff)

Matches if the left hand side is more than $cutoff.

at_least($cutoff)

Matches if the left hand side is at least $cutoff.

less_than($cutoff)

Matches if the left hand side is less than $cutoff.

at_most($cutoff)

Matches if the left hand side is at most $cutoff.

positive()

This is a synonym for more_than(0).

negative()

This is a synonym for less_than(0).

range($bottom, $top)

A synonym for all(at_least($bottom, at_most($top)).

numwise($number, ...)

Matches the left hand side numerically with $number if that makes sense, returns false otherwise. If given multiple numbers it will return multiple matchers.

string()

Matches any string, that is any defined value that's that's not a reference without string overloading.

stringwise($string, ...)

Matches the left hand side lexographically with $string if that makes sense, returns false otherwise. If given multiple strings it will return multiple matchers.

string_length($matcher)

Matches the string's length

object()

Matches any object (that is, a blessed reference).

instance_of($class)

Matches any instance of class $class.

ref_type($type)

Matches any unblessed reference of type $type.

array()

Matches any unblessed array.

array_length($matcher)

Matches any unblessed array whose length matches $matcher.

tuple(@entries)

Matches a list whose elements match @entries one by one.

head(@elements)

Matches a list whose head elements match @entries one by one.

sequence($matcher)

Matches a list whose elements all match $matcher.

contains(@matcher)

Matches a list that for all of @matchers contains a matching element.

sorted($matcher)

Sorts a list and matches it against $matcher.

sorted_by($sorter, $matcher)

Sorts a list using $sorter and matches it against $matcher.

hash()

Matches any unblessed hash.

hashwise($hashref)

Matches a hash for against $hashref. The keys must be identical, and all keys in the hash much smartmatch the keys in $hashref.

hash_keys($matcher)

Match a list of hash keys against $matcher.

hash_values($matches)

Match a list of hash values against $matcher

sub_hash({ key => $matcher, ... })

Matches each hash entry against a the corresponding matcher.

match { ... }

Create a new matching function. It will be run with the left-hand side in $_.

delegate { ... } $matcher

Run the block, and then match the return value against $matcher.

address($var)

Matches if the left hand side has the same address as $var.

value

This with match on recursive value equivalence. It will use other matchers such as tuple, hashwise and address to achieve this.

AUTHOR ^

Leon Timmermans <leont@cpan.org>

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2011 by Leon Timmermans.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: