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 on scalar values. See Venus::Gather for operating on collections of data, e.g. array references.

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:

Venus::Kind::Utility

INTEGRATES

This package integrates behaviors from:

Venus::Role::Accessible

Venus::Role::Buildable

Venus::Role::Valuable

METHODS

This package provides the following methods:

clear

clear() (Venus::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) (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

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(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.

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(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

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(any | coderef $code) (Venus::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) (Venus::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 $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.

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]
result example 4
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"
result example 5
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

then(any | coderef $code) (Venus::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(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.

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"

where

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

where example 1
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"
where example 2
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"
where example 3
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

AUTHORS

Awncorp, awncorp@cpan.org

LICENSE

Copyright (C) 2022, 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.