NAME
Data::Hopen::Util::NameSet - set of names (strings or regexps)
SYNOPSIS
NameSet stores strings and regexps, and can quickly tell you whether a given string matches one of the stored strings or regexps.
FUNCTIONS
new
Create a new instance. Usage: Data::Hopen::Util::Nameset->new(...)
. The parameters are as "add".
add
Add one or more strings or regexps to the NameSet. Usage:
$instance->add(x1, x2, ...)
where each xn
can be a scalar, regexp, arrayref (processed recursively) or hashref (the keys are added and the values are ignored).
contains
Return truthy if the NameSet contains the argument. Usage: $set->contains('foo')
.
smartmatch overload
For convenience, 'foo' ~~ $nameset
invokes $nameset->contains('foo')
. This is inspired by the Raku behaviour, in which $x ~~ $y
calls $y.ACCEPTS($x)
NOTE: $nameset ~~ 'foo'
(object first) is officially not supported by this module. This form is possible in stable perls at least through 5.26. However, the changes (since reverted) in 5.27.7 would not have supported this form. See http://blogs.perl.org/users/leon_timmermans/2017/12/smartmatch-in-5277.html. However, as far as I can tell, even 5.27.7 would have supported the 'foo' ~~ $nameset
form.
strings
Accessor for the strings in the set. Returns an arrayref.
regexps
Accessor for the regexps in the set. Returns an arrayref.
complex
Returns truthy if the nameset has any regular expressions.
_build
(Internal) Build a regex from all the strings and regexps in the set. Returns the new regexp --- does not mutate $self.
In the current implementation, strings are matched case-sensitively. Regexps are matched with whatever flags they were compiled with.