Venus::Gather

Gather Class

Gather Class for Perl 5

method: clear method: count method: data method: defined method: expr method: just method: new method: none method: object method: only method: reduce method: result method: skip method: take method: test method: then method: type method: when method: where

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->when(sub{$_ eq 1})->then(sub{"one"});
$gather->when(sub{$_ eq 2})->then(sub{"two"});

$gather->none(sub{"?"});

my $result = $gather->result;

# ["?"]

This package provides an object-oriented interface for complex pattern matching operations on collections of data, e.g. array references. See Venus::Match for operating on scalar values.

Venus::Kind::Utility

Venus::Role::Accessible Venus::Role::Buildable Venus::Role::Valuable

on_none: rw, opt, CodeRef, sub{} on_only: rw, opt, CodeRef, sub{1} on_then: rw, opt, ArrayRef[CodeRef], [] on_when: rw, opt, ArrayRef[CodeRef], []

The clear method resets all gather conditions and returns the invocant.

clear() (Venus::Gather)

{ since => '1.55', }

=example-1 clear

# given: synopsis

package main;

my $clear = $gather->clear;

# bless(..., "Venus::Gather")

The count method calls "result" and returns the number of items gathered.

count(any $data) (number)

{ since => '4.15', }

=example-1 count

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->expr(qr/^t/)->take;

my $result = $gather->count;

# 2

The data method takes a hashref (i.e. lookup table) and creates gather conditions and actions based on the keys and values found.

data(hashref $data) (Venus::Gather)

{ since => '1.55', }

=example-1 data

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->data({
  "one" => 1,
  "two" => 2,
  "three" => 3,
  "four" => 4,
  "five" => 5,
  "six" => 6,
  "seven" => 7,
  "eight" => 8,
  "nine" => 9,
  "zero" => 0,
});

my $result = $gather->none('?')->result;

# [1..9, 0]

The defined method registers a "when" condition which only allows matching if the value presented is defined.

defined() (Venus::Gather)

{ since => '4.15', }

=example-1 defined

package main;

use Venus::Gather;

my $gather = Venus::Gather->new;

$gather->defined->take;

my $result = $gather->result([0, "", undef, false]);

# [0, "", false]

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.

expr(string | regexp $expr) (Venus::Gather)

{ since => '1.55', }

=example-1 expr

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->expr('one')->then(sub{[split //]});

my $result = $gather->result;

# [["o", "n", "e"]]

The just method registers a "when" condition that check if the match value is an exact string match of the $topic provided.

just(string $topic) (Venus::Gather)

{ since => '1.55', }

=example-1 just

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->just('one')->then(1);
$gather->just('two')->then(2);
$gather->just('three')->then(3);

my $result = $gather->result;

# [1,2,3]

The new method constructs an instance of the package.

new(any @args) (Venus::Gather)

{ since => '4.15', }

The none method registers a special condition that returns a result only when no other conditions have been matched.

none(any | coderef $code) (Venus::Gather)

{ since => '1.55', }

=example-1 none

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->just('ten')->then(10);

$gather->none('none');

my $result = $gather->result;

# ["none"]

The object method registers a "when" condition which only allows matching if the value presented is an object.

object() (Venus::Gather)

{ since => '4.15', }

=example-1 object

package main;

use Venus::Gather;

my $gather = Venus::Gather->new;

$gather->object->take;

my $result = $gather->result([0, "", undef, false, bless{}]);

# [bless({}, 'main')]

The only method registers a special condition that only allows matching on the value only if the code provided returns truthy.

only(coderef $code) (Venus::Gather)

{ since => '1.55', }

=example-1 only

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->only(sub{grep /^[A-Z]/, @$_});

$gather->just('one')->then(1);

my $result = $gather->result;

# []

The reduce method returns a new Venus::Gather object using the values returned via the "result" method.

reduce(any $data) (Venus::Gather)

{ since => '4.15', }

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.

result(any $data) (any)

{ since => '1.55', }

=example-1 result

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->just('one')->then(1);
$gather->just('six')->then(6);

my $result = $gather->result;

# [1,6]

The test method evaluates the registered conditions and returns truthy if a match can be made, without executing any of the actions (i.e. the "then" code) or the special "none" condition.

test() (boolean)

{ since => '1.55', }

=example-1 test

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->just('one')->then(1);
$gather->just('six')->then(6);

my $test = $gather->test;

# 2

The skip method registers a "then" condition which ignores (i.e. skips) the matched line item.

skip() (Venus::Gather)

{ since => '1.55', }

=example-1 skip

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->expr(qr/e$/)->skip;

$gather->expr(qr/.*/)->take;

my $result = $gather->result;

# ["two", "four", "six", "seven", "eight", "zero"]

The take method registers a "then" condition which returns (i.e. takes) the matched line item as-is.

take() (Venus::Gather)

{ since => '1.55', }

=example-1 take

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->expr(qr/e$/)->take;

my $result = $gather->result;

# ["one", "three", "five", "nine"]

The then method registers an action to be executed if the corresponding gather condition returns truthy.

then(any | coderef $code) (Venus::Gather)

{ since => '1.55', }

=example-1 then

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->just('one');
$gather->then(1);

$gather->just('two');
$gather->then(2);

my $result = $gather->result;

# [1,2]

The type method accepts a "type expression" and registers a "when" condition which matches values conforming to the type expression specified.

type(string $expr) (Venus::Gather)

{ since => '4.15', }

=example-1 type

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([1, "1"]);

$gather->type('string');
$gather->then('string');

$gather->type('number');
$gather->then('number');

my $result = $gather->result;

# ["number", "string"]

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.

when(string | coderef $code, any @args) (Venus::Gather)

{ since => '1.55', }

=example-1 when

package main;

use Venus::Gather;

my $gather = Venus::Gather->new([
  "one",
  "two",
  "three",
  "four",
  "five",
  "six",
  "seven",
  "eight",
  "nine",
  "zero",
]);

$gather->when(sub{$_ eq 'one'});
$gather->then(1);

$gather->when(sub{$_ eq 'two'});
$gather->then(2);

$gather->when(sub{$_ eq 'six'});
$gather->then(6);

my $result = $gather->result;

# [1,2,6]

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.

where() (Venus::Gather)

{ since => '1.55', }

=example-1 where

package main;

use Venus::Gather;

my $gather = Venus::Gather->new;

my $subgather1 = $gather->expr(qr/^p([a-z]+)ch/)->where;

$subgather1->just('peach')->then('peach-123');
$subgather1->just('patch')->then('patch-456');
$subgather1->just('punch')->then('punch-789');

my $subgather2 = $gather->expr(qr/^m([a-z]+)ch/)->where;

$subgather2->just('merch')->then('merch-123');
$subgather2->just('march')->then('march-456');
$subgather2->just('mouch')->then('mouch-789');

my $result = $gather->result(['peach', 'preach']);

# ["peach-123"]

t/Venus.t: present: authors t/Venus.t: present: license

85 POD Errors

The following errors were encountered while parsing the POD:

Around line 14:

Unknown directive: =name

Around line 22:

Unknown directive: =tagline

Around line 30:

Unknown directive: =abstract

Around line 38:

Unknown directive: =includes

Around line 64:

Unknown directive: =synopsis

Around line 102:

Unknown directive: =description

Around line 112:

Unknown directive: =inherits

Around line 120:

Unknown directive: =integrates

Around line 130:

Unknown directive: =attributes

Around line 141:

Unknown directive: =method

Around line 145:

Unknown directive: =signature

Around line 149:

Unknown directive: =metadata

Around line 184:

Unknown directive: =method

Around line 188:

Unknown directive: =signature

Around line 192:

Unknown directive: =metadata

Around line 233:

Unknown directive: =method

Around line 238:

Unknown directive: =signature

Around line 242:

Unknown directive: =metadata

Around line 321:

=cut found outside a pod block. Skipping to next block.

Around line 331:

Unknown directive: =method

Around line 336:

Unknown directive: =signature

Around line 340:

Unknown directive: =metadata

Around line 370:

Unknown directive: =method

Around line 376:

Unknown directive: =signature

Around line 380:

Unknown directive: =metadata

Around line 446:

=cut found outside a pod block. Skipping to next block.

Around line 456:

Unknown directive: =method

Around line 461:

Unknown directive: =signature

Around line 465:

Unknown directive: =metadata

Around line 536:

=cut found outside a pod block. Skipping to next block.

Around line 573:

=cut found outside a pod block. Skipping to next block.

Around line 583:

Unknown directive: =method

Around line 587:

Unknown directive: =signature

Around line 591:

Unknown directive: =metadata

Around line 609:

=cut found outside a pod block. Skipping to next block.

Around line 640:

=cut found outside a pod block. Skipping to next block.

Around line 683:

=cut found outside a pod block. Skipping to next block.

Around line 705:

Unknown directive: =method

Around line 710:

Unknown directive: =signature

Around line 714:

Unknown directive: =metadata

Around line 795:

=cut found outside a pod block. Skipping to next block.

Around line 816:

Unknown directive: =method

Around line 821:

Unknown directive: =signature

Around line 825:

Unknown directive: =metadata

Around line 856:

Unknown directive: =method

Around line 861:

Unknown directive: =signature

Around line 865:

Unknown directive: =metadata

Around line 940:

=cut found outside a pod block. Skipping to next block.

Around line 955:

Unknown directive: =method

Around line 960:

Unknown directive: =signature

Around line 964:

Unknown directive: =metadata

Around line 997:

=cut found outside a pod block. Skipping to next block.

Around line 1009:

Unknown directive: =method

Around line 1017:

Unknown directive: =signature

Around line 1021:

Unknown directive: =metadata

Around line 1089:

=cut found outside a pod block. Skipping to next block.

Around line 1126:

=cut found outside a pod block. Skipping to next block.

Around line 1163:

=cut found outside a pod block. Skipping to next block.

Around line 1199:

=cut found outside a pod block. Skipping to next block.

Around line 1209:

Unknown directive: =method

Around line 1215:

Unknown directive: =signature

Around line 1219:

Unknown directive: =metadata

Around line 1287:

=cut found outside a pod block. Skipping to next block.

Around line 1297:

Unknown directive: =method

Around line 1302:

Unknown directive: =signature

Around line 1306:

Unknown directive: =metadata

Around line 1349:

Unknown directive: =method

Around line 1354:

Unknown directive: =signature

Around line 1358:

Unknown directive: =metadata

Around line 1399:

Unknown directive: =method

Around line 1404:

Unknown directive: =signature

Around line 1408:

Unknown directive: =metadata

Around line 1483:

=cut found outside a pod block. Skipping to next block.

Around line 1493:

Unknown directive: =method

Around line 1499:

Unknown directive: =signature

Around line 1503:

Unknown directive: =metadata

Around line 1537:

Unknown directive: =method

Around line 1545:

Unknown directive: =signature

Around line 1549:

Unknown directive: =metadata

Around line 1597:

Unknown directive: =method

Around line 1603:

Unknown directive: =signature

Around line 1607:

Unknown directive: =metadata

Around line 1671:

=cut found outside a pod block. Skipping to next block.

Around line 1705:

=cut found outside a pod block. Skipping to next block.

Around line 1715:

Unknown directive: =partials