> On Fri, Nov 6, 2020 at 8:23 AM Paul Procacci <pproca...@gmail.com> wrote:
> >
> > So two example patterns are:
> >
> > [\\u0009\\u000A\\u000D\\u0020-\\u007E\\u0085\\u00A0-\\uD7FF\\uE000-\\uFFFD\\u10000-\\u10FFFF]*
> > [\\p{L}\\p{Z}\\p{N}_.:\\/=+\\-@]*
> >
> > To note, the RE's themselves cannot be changed as they are fed externally.
> > Given that I'm stuck with these RE's which I believe are PCRE, It was my 
> > hopes to lean on perl to do the evaluation.
> > Raku's perl regex engine is too old to interpret it properly, hence the 
> > shenanigans with Inline::Perl5.

—snip—

This came out of the San Francisco Raku study group, just now.

# This part is the set-up to allow interpolation of the P5 regex into a 
callable sub.
use Inline::Perl5;
use MONKEY-SEE-NO-EVAL;
my $regex_string = '\w'; # In reality, this comes from a file!

# Returns 1e0 when True and Nil when False.
my $is_valid_sub = Inline::Perl5.new.run("
    sub \{
        return \$_[0] =~ /$regex_string/;
    \}
");
# Wrapper to allow naming the sub (instead of anonymous sub in a variable),
# and forces the return values to be the clearer Bool::True and Bool::False.
sub is-valid ( $candidate --> Bool ) {
    return ? $is_valid_sub.($candidate);
}

# Test what we have so far.
say is-valid('abc').raku;
say is-valid('^').raku;

# You don't really need a subtype
multi sub real-use-of-where ( $var1 where { is-valid($^var1) } ) {
    say "This is definitely valid: $var1";
}
multi sub real-use-of-where ( $var1 ) {
    say "Not valid: $var1";
}
# Test it.
real-use-of-where('abc');
real-use-of-where('^');

# Now make the subset, and use it as a type. The `where` is no longer needed.
subset test-set of Str where *.&is-valid;
sub real-use-of-subsets ( test-set $var1 ) {
    say "This is definitely a `test-set`: $var1";
}
sub real-use-of-subsets ( test-set $var1 ) {
    say "Not a `test-set`: $var1";
}
# Test it.
real-use-of-subsets('abc');
real-use-of-subsets('^’);

— 
Hope this helps,
Bruce Gray (Util of PerlMonks)

Reply via email to