develooper Front page | perl.perl6.users | Postings from September 2019

Variable character class

Thread Next
From:
yary
Date:
September 1, 2019 01:54
Subject:
Variable character class
Message ID:
CAG2CFAZJtvOoYiffZWa-RRk1Xiu78oKvo7uS8P6efksS4RoD7g@mail.gmail.com
I found something easy in Perl 5 that's puzzling me in Perl 6- specifying a
character class via a variable.

Perl 5:
sub matching_chars {
  (my $chars_to_match, local $_) = @_;
  /([$chars_to_match]+)/
}

say matching_chars('24680', '19584203'); # says 8420
say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//

Perl 6:
sub matching_chars(Str $chars_to_match, Str $_) {
    # warnings, treats as string not variable
    m/<[$chars_to_match]>/;
}

How do I get Perl 6 to interpret a variable in the contents of a character
class?
From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think
that  Rakudo would use the literal contents of $chars_to_match, instead
it's using the literal chars "$ c h a r s _ t o _ m a t c h" and warning
about repeated c, underscore, etc.

-y

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About