On Wed, 4 Sep 2019 21:44:29 -0700
William Michels via perl6-users <[email protected]> wrote:
> Hi Gianni, I'm not sure of the Perl5 case, but what you're saying is,
> if your target string is backslashed, be sure to "quote-interpolate
> it" in Perl6? (see below):
Re-reading what I wrote, I realise it was really not clear. Let me try
again.
yary's original code was::
sub matching_chars {
(my $chars_to_match, local $_) = @_;
/([$chars_to_match]+)/
}
say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
If ``matching_chars`` took "a set of characters expressed as a string"
as its first argument, you'd expect to be able to say::
say matching_chars('+/][', 'Apple ][+//e');
but this doesn't work, because it tells Perl5 to compile this regex::
([+/][])+
which doesn't compile ("Unmatched [ in regex").
Sidhekin's Perl6 version doesn't have this issue. On the other hand,
this happens::
sub matching_chars(Str $chars_to_match, Str $target) {
my @chars-to-match = $chars_to_match.comb;
return $target ~~ m:g/@chars-to-match + /;
}
is matching_chars('+/][', Q{Apple ][+//\\e}),['][+//'];
is matching_chars('+\/][', Q{Apple ][+//\\e}),[Q{][+//\\}];
The "fixed" Perl5 version would be::
/([\Q$chars_to_match\E]+)/
or, equivalently::
$chars_to_match = quotemeta($chars_to_match);
/([$chars_to_match]+)/
Does that help?
--
Dakkar - <Mobilis in mobile>
GPG public key fingerprint = A071 E618 DD2C 5901 9574
6FE2 40EA 9883 7519 3F88
key id = 0x75193F88
You have literary talent that you should take pains to develop.