Venus::Match - Match Class
Match Class for Perl 5
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; # "?"
This package provides an object-oriented interface for complex pattern matching operations on scalar values. See Venus::Gather for operating on collections of data, e.g. array references.
This package has the following attributes:
on_none(CodeRef)
This attribute is read-write, accepts (CodeRef) values, is optional, and defaults to sub{}.
(CodeRef)
sub{}
on_only(CodeRef)
This attribute is read-write, accepts (CodeRef) values, is optional, and defaults to sub{1}.
sub{1}
on_then(ArrayRef[CodeRef])
This attribute is read-write, accepts (ArrayRef[CodeRef]) values, is optional, and defaults to [].
(ArrayRef[CodeRef])
[]
on_when(ArrayRef[CodeRef])
This package inherits behaviors from:
Venus::Kind::Utility
This package integrates behaviors from:
Venus::Role::Accessible
Venus::Role::Buildable
Venus::Role::Valuable
This package provides the following methods:
clear() (Venus::Match)
The clear method resets all match conditions and returns the invocant.
Since 1.23
1.23
# given: synopsis package main; my $clear = $match->clear; # bless(..., "Venus::Match")
data(hashref $data) (Venus::Match)
The data method takes a hashref (i.e. lookup table) and creates match conditions and actions based on the keys and values found.
Since 0.07
0.07
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"
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(string | regexp $expr) (Venus::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.
$topic
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"]
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(string $topic) (Venus::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
0.03
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"
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"
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"
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"
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"
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(any | coderef $code) (Venus::Match)
The none method registers a special condition that returns a result only when no other conditions have been matched.
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"
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(coderef $code) (Venus::Match)
The only method registers a special condition that only allows matching on the match value only if the code provided returns truthy.
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
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(any $data) (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. Optionally, when passed an argument this method assign the argument as the value/topic and then perform the operation.
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)
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]
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('b'); # "b"
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('z'); # undef
then(any | coderef $code) (Venus::Match)
The then method registers an action to be executed if the corresponding match condition returns truthy.
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"
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(string | coderef $code, any @args) (Venus::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.
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"
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"
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"
where() (Venus::Match)
The where method registers an action as a sub-match operation, to be executed if the corresponding match condition returns truthy. This method returns the sub-match object.
Since 1.40
1.40
package main; use Venus::Match; my $match = Venus::Match->new; my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where; $submatch1->just('peach')->then('peach-123'); $submatch1->just('patch')->then('patch-456'); $submatch1->just('punch')->then('punch-789'); my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where; $submatch2->just('merch')->then('merch-123'); $submatch2->just('march')->then('march-456'); $submatch2->just('mouch')->then('mouch-789'); my $result = $match->result('peach'); # "peach-123"
package main; use Venus::Match; my $match = Venus::Match->new; my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where; $submatch1->just('peach')->then('peach-123'); $submatch1->just('patch')->then('patch-456'); $submatch1->just('punch')->then('punch-789'); my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where; $submatch2->just('merch')->then('merch-123'); $submatch2->just('march')->then('march-456'); $submatch2->just('mouch')->then('mouch-789'); my $result = $match->result('march'); # "march-456"
package main; use Venus::Match; my $match = Venus::Match->new; my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where; $submatch1->just('peach')->then('peach-123'); $submatch1->just('patch')->then('patch-456'); $submatch1->just('punch')->then('punch-789'); my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where; $submatch2->just('merch')->then('merch-123'); $submatch2->just('march')->then('march-456'); $submatch2->just('mouch')->then('mouch-789'); my $result = $match->result('pirch'); # undef
Awncorp, awncorp@cpan.org
awncorp@cpan.org
Copyright (C) 2000, Awncorp, awncorp@cpan.org.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.
To install Venus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Venus
CPAN shell
perl -MCPAN -e shell install Venus
For more information on module installation, please visit the detailed CPAN module installation guide.