NAME
Data::TagListMatcher - A simple rule based tag matcher
VERSION
version v0.02
SYNOPSIS
use Data::TagListMatcher;
my Data::TagListMatcher $matcher = Data::TagListMatcher->new;
$matcher->add_metric(score => int => 'add');
$matcher->add_rule(good => 5);
my $res = $matcher->match(['ok', 'good', 'funny']);
This package provides a simple rule based tag matcher.
This package inherits from Data::Identifier::Interface::Subobjects.
METHODS
new
my Data::TagListMatcher $matcher = Data::TagListMatcher->new;
(since v0.01)
Creates a new matcher object. Takes no options.
default_converter
$matcher->default_converter($converter);
my $converter = $matcher->default_converter;
(since v0.02)
A function that is called to convert tags from strings into objects as needed.
The function is passed the matcher as first argument and the string to convert as second argument. The function must return any object that "eq" in Data::Identifier can compare.
If the function dies the tag is handled the same way as if the converter was not set. However it might be called again for the same tag or other tags later.
The result might be cached.
It is undefined in which order the converter is tried related to other ways to convert from a string to an object.
See also: "default_type".
default_type
$matcher->default_type($type);
my Data::Identifier $type = $matcher->default_type;
(since v0.02)
The type to use when a tag needs to be converted from a string to an object. When set this will be used to call "new" in Data::Identifier with the given type and the string as identifier.
The type passed here is converted to a Data::Identifier as needed by "new" in Data::Identifier using from.
If converting to an identifier fails the tag is handled the same way as if the type was not set. However it might be tried again for the same tag or other tags later.
The result might be cached.
It is undefined in which order the type is tried related to other ways to convert from a string to an object.
See also: "default_converter".
clear_cache
$matcher->clear_cache;
(since v0.02)
Clears all caches.
register
$matcher->register;
(since v0.02)
Registers identifiers currently known to the matcher. This may improve performance when calling "match" multiple times.
However registered identifiers are kept globally and this operation cannot be undone.
This operation may interact with the cache (both prefil it, but also register any already cached entries).
See also: "register" in Data::Identifier.
add_metric
$matcher->add_metric($name, $type, $operation);
(since v0.01)
Adds a metric to the matcher.
Each metric has a name by which it is known, a type of it's value, and an operation that defines how scores from rules are applied.
add_rule
$matcher->add_rule($match, $score);
(since v0.01)
Adds a rule to the matcher.
$match must be a tag or an arrayref of tags to check. The rule matches if all tags are present.
$score is the score the rule applies to the metrics. It must be an hashref. Each element in the hashref uses the key of the metric it applies to and the value that is applied to the metric based on the selected operator.
There is a shortcut that if $score is a single non-ref and there is only one metric it is then applied to the single metric present.
Note: Rules need to be added after the metrics they apply to.
match
my $score = $matcher->match($list);
(since v0.01)
Runs the matcher agains the given list (arrayref). The return will be a hashref with one element for each metric added via "add_metric".
AUTHOR
Philipp Schafft <lion@cpan.org>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2026 by Philipp Schafft <lion@cpan.org>.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)