NAME
Venus::Match - Match Class
ABSTRACT
Match Class for Perl 5
SYNOPSIS
package main;
use Venus::Match;
my $match = Venus::Match->new(5);
$match->when(sub{$_ < 5})->then(sub{"< 5"});
$match->when(sub{$_ > 5})->then(sub{"> 5"});
$match->none(sub{"?"});
my $result = $match->result;
# "?"
DESCRIPTION
This package provides an object-oriented interface for complex pattern matching operations.
ATTRIBUTES
This package has the following attributes:
on_none
on_none(CodeRef)
This attribute is read-write, accepts (CodeRef)
values, is optional, and defaults to sub{}
.
on_only
on_only(CodeRef)
This attribute is read-write, accepts (CodeRef)
values, is optional, and defaults to sub{1}
.
on_then
on_then(ArrayRef[CodeRef])
This attribute is read-write, accepts (ArrayRef[CodeRef])
values, is optional, and defaults to []
.
on_when
on_when(ArrayRef[CodeRef])
This attribute is read-write, accepts (ArrayRef[CodeRef])
values, is optional, and defaults to []
.
INHERITS
This package inherits behaviors from:
INTEGRATES
This package integrates behaviors from:
METHODS
This package provides the following methods:
clear
clear() (Match)
The clear method resets all match conditions and returns the invocant.
Since 1.23
- clear example 1
-
# given: synopsis package main; my $clear = $match->clear; # bless(..., "Venus::Match")
data
data(HashRef $data) (Match)
The data method takes a hashref (i.e. lookup table) and match conditions and actions based on the keys and values found.
Since 0.07
- data example 1
-
package main; use Venus::Match; my $match = Venus::Match->new('a'); $match->data({ 'a' => 'b', 'c' => 'd', 'e' => 'f', 'g' => 'h', }); my $result = $match->none('z')->result; # "b"
- data example 2
-
package main; use Venus::Match; my $match = Venus::Match->new('x'); $match->data({ 'a' => 'b', 'c' => 'd', 'e' => 'f', 'g' => 'h', }); my $result = $match->none('z')->result; # "z"
expr
expr(Str | RegexpRef $expr) (Match)
The expr method registers a "when" condition that check if the match value is an exact string match of the $topic
if the topic is a string, or that it matches against the topic if the topic is a regular expression.
Since 0.07
- expr example 1
-
package main; use Venus::Match; my $match = Venus::Match->new('1901-01-01'); $match->expr('1901-01-01')->then(sub{[split /-/]}); my $result = $match->result; # ["1901", "01", "01"]
- expr example 2
-
package main; use Venus::Match; my $match = Venus::Match->new('1901-01-01'); $match->expr(qr/^1901-/)->then(sub{[split /-/]}); my $result = $match->result; # ["1901", "01", "01"]
just
just(Str $topic) (Match)
The just method registers a "when" condition that check if the match value is an exact string match of the $topic
provided.
Since 0.03
- just example 1
-
package main; use Venus::Match; my $match = Venus::Match->new('a'); $match->just('a')->then('a'); $match->just('b')->then('b'); $match->just('c')->then('c'); my $result = $match->result; # "a"
- just example 2
-
package main; use Venus::Match; use Venus::String; my $match = Venus::Match->new(Venus::String->new('a')); $match->just('a')->then('a'); $match->just('b')->then('b'); $match->just('c')->then('c'); my $result = $match->result; # "a"
- just example 3
-
package main; use Venus::Match; use Venus::String; my $match = Venus::Match->new(Venus::String->new('c')); $match->just('a')->then('a'); $match->just('b')->then('b'); $match->just('c')->then('c'); my $result = $match->result; # "c"
- just example 4
-
package main; use Venus::Match; my $match = Venus::Match->new(1.23); $match->just('1.230')->then('1.230'); $match->just(01.23)->then('123'); $match->just(1.230)->then(1.23); my $result = $match->result; # "1.23"
- just example 5
-
package main; use Venus::Match; use Venus::Number; my $match = Venus::Match->new(Venus::Number->new(1.23)); $match->just('1.230')->then('1.230'); $match->just(01.23)->then('123'); $match->just(1.230)->then(1.23); my $result = $match->result; # "1.23"
- just example 6
-
package main; use Venus::Match; use Venus::Number; my $match = Venus::Match->new(1.23); $match->just(Venus::Number->new('1.230'))->then('1.230'); $match->just(Venus::Number->new(01.23))->then('123'); $match->just(Venus::Number->new(1.230))->then(1.23); my $result = $match->result; # "1.23"
none
none(CodeRef $code) (Match)
The none method registers a special condition that returns a result only when no other conditions have been matched.
Since 0.03
- none example 1
-
package main; use Venus::Match; my $match = Venus::Match->new('z'); $match->just('a')->then('a'); $match->just('b')->then('b'); $match->just('c')->then('c'); $match->none('z'); my $result = $match->result; # "z"
- none example 2
-
package main; use Venus::Match; my $match = Venus::Match->new('z'); $match->just('a')->then('a'); $match->just('b')->then('b'); $match->just('c')->then('c'); $match->none(sub{"($_) not found"}); my $result = $match->result; # "(z) not found"
only
only(CodeRef $code) (Match)
The only method registers a special condition that only allows matching on the match value only if the code provided returns truthy.
Since 0.03
- only example 1
-
package main; use Venus::Match; my $match = Venus::Match->new(5); $match->only(sub{$_ != 5}); $match->just(5)->then(5); $match->just(6)->then(6); my $result = $match->result; # undef
- only example 2
-
package main; use Venus::Match; my $match = Venus::Match->new(6); $match->only(sub{$_ != 5}); $match->just(5)->then(5); $match->just(6)->then(6); my $result = $match->result; # 6
result
result() (Any)
The result method evaluates the registered conditions and returns the result of the action (i.e. the "then" code) or the special "none" condition if there were no matches. In list context, this method returns both the result and whether or not a condition matched.
Since 0.03
- result example 1
-
package main; use Venus::Match; my $match = Venus::Match->new('a'); $match->just('a')->then('a'); $match->just('b')->then('b'); $match->just('c')->then('c'); my $result = $match->result; # "a"
- result example 2
-
package main; use Venus::Match; my $match = Venus::Match->new('a'); $match->just('a')->then('a'); $match->just('b')->then('b'); $match->just('c')->then('c'); my ($result, $matched) = $match->result; # ("a", 1)
- result example 3
-
package main; use Venus::Match; sub fibonacci { my ($n) = @_; my $match = Venus::Match->new($n) ->just(1)->then(1) ->just(2)->then(1) ->none(sub{fibonacci($n - 1) + fibonacci($n - 2)}) ->result } my $result = [fibonacci(4), fibonacci(6), fibonacci(12)] # [3, 8, 144]
then
then(CodeRef $code) (Match)
The then method registers an action to be executed if the corresponding match condition returns truthy.
Since 0.03
- then example 1
-
package main; use Venus::Match; my $match = Venus::Match->new('b'); $match->just('a'); $match->then('a'); $match->just('b'); $match->then('b'); my $result = $match->result; # "b"
- then example 2
-
package main; use Venus::Match; my $match = Venus::Match->new('b'); $match->just('a'); $match->then('a'); $match->just('b'); $match->then('b'); $match->then('x'); my $result = $match->result; # "x"
when
when(Str | CodeRef $code, Any @args) (Match)
The when method registers a match condition that will be passed the match value during evaluation. If the match condition returns truthy the corresponding action will be used to return a result. If the match value is an object, this method can take a method name and arguments which will be used as a match condition.
Since 0.03
- when example 1
-
package main; use Venus::Match; my $match = Venus::Match->new('a'); $match->when(sub{$_ eq 'a'}); $match->then('a'); $match->when(sub{$_ eq 'b'}); $match->then('b'); $match->when(sub{$_ eq 'c'}); $match->then('c'); my $result = $match->result; # "a"
- when example 2
-
package main; use Venus::Match; use Venus::Type; my $match = Venus::Match->new(Venus::Type->new(1)->deduce); $match->when('isa', 'Venus::Number'); $match->then('Venus::Number'); $match->when('isa', 'Venus::String'); $match->then('Venus::String'); my $result = $match->result; # "Venus::Number"
- when example 3
-
package main; use Venus::Match; use Venus::Type; my $match = Venus::Match->new(Venus::Type->new('1')->deduce); $match->when('isa', 'Venus::Number'); $match->then('Venus::Number'); $match->when('isa', 'Venus::String'); $match->then('Venus::String'); my $result = $match->result; # "Venus::String"