Hi Gianni,

I think we're looking at two different aspects of the same problem.
I'm just concerned with learning Perl6 regexes. Presumably many people
will continue to use Perl5 regexes for a long time to come (for
reasons of familiarity, and/or speed).

Regarding Perl6 regexes, your last two P6 examples are below, with
added variants. I don't see any difference between wrapping the string
that becomes $target in single-quotes or with Q{...}. See first four
examples below:

say matching_chars('+/][', 'Apple ][+//\\e'); #says (「][+//」)
say matching_chars('+/][', Q{Apple ][+//\\e}); #says (「][+//」)
say matching_chars('+\/][', 'Apple ][+//\\e'); #says (「][+//\\」)
say matching_chars('+\/][', Q{Apple ][+//\\e}); #says (「][+//\\」)

OTOH, I can eliminate backslashes from the smartmatch result by simply
double-quoting the input string that becomes $chars_to_match (third
and fourth examples below). At the same time, double-quoting the
string that becomes $target doesn't alter the 'goal' of eliminating
backslashes from the smartmatch result:

say matching_chars('+\/][', 'Apple ][+//\\e'); #says (「][+//\」)
say matching_chars('+\/][', "Apple ][+//\\e"); #says (「][+//\」)
say matching_chars("+\/][", 'Apple ][+//\\e'); #says (「][+//」)
say matching_chars("+\/][", "Apple ][+//\\e"); #says (「][+//」)

This P6 way seems pretty clever--and also easy to remember--to me.
Does it seem that way to you? Or do you still find P5 more suitable?

Best Regards, Bill.



On Thu, Sep 5, 2019 at 6:25 AM Gianni Ceccarelli <dak...@thenautilus.net> wrote:
>
> On Wed, 4 Sep 2019 21:44:29 -0700
> William Michels via perl6-users <perl6-users@perl.org> 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.

Reply via email to