On 01/17/2016 06:07 PM, Tom Browder wrote:
> I'm trying to write all new Perl code in Perl 6.  One thing I need is
> the equivalent of the Perl 5 qr// and, following Perl 6 docs, I've
> come to use something like this (I'm trying to ignore certain
> directories):
> 
> # regex of dirs to ignore
> my regex dirs {
>   \/home\/user\/\.cpan |
>   \/home\/tbrowde\/\.emacs
> }

Better written as

my regex dirs {
   | '/home/user/.cpan'
   | '/home/tbowde/.emacs'
}

Yes, quoting does now work in regexes too. Cool, right? :-)

(The leading | is ignored, it just allows you to format the alternation
more consistently)

> for "dirlist.txt".IO.lines -> $line {
>   # ignore certain dirs
>   if $line ~~ m{<dirs>} {
>      next;
>   }
> }
> 
> My question: Is there a way to have Perl 6 do the required escaping
> for the regex programmatically, i.e., turn this:
> 
> my $str = '/home/usr/.cpan';
> 
> into this:
> 
> my regex dirs {
>   \/home\/usr\/\.cpan
> }
> 
> automatically?

Even better: No need to escape anymore. If you use $str in a regex, and
it actually contains a Str, it is always taken to match literally, so


my $dir1 = '/home/user/.cpan';
my $dir2 = '/home/tbowde/.emacs';
my regex ignore_dirs { $dir1 | $dir2 }

does what you want.

If you want the interpolated string to be treated as a regex, you have
to use it as   my regex dirs { <$dir1> }.

Cheers,
Moritz

Reply via email to