Perl 5.30 fixes single quoted qr” with \N{}

The qr// operator allows you to compile a regular expression without applying it to anything. You get the pattern without the match, and you can reuse the pattern as often as you like. Before v5.30, it had an inconsistency with \N{} sequences, but that’s fixed now.

I covered part of this in Understand the order of operations in double quoted contexts. The qr// is a double-quoted context, but you can make it a single-quoted context with the single tick as a delimiter:

my $string = '\ABuster\s+';
say "String: $string";

# will interpolate
my $double = qr/$string/;
say "Double: $double";

# will not interpolate
my $single = qr'$string';
say "Single: $single";

The output shows that qr// interpolates $string, but qr'' does not:

String: \ABuster\s+
Double: (?^:\ABuster\s+)
Single: (?^:$string)

Here’s another program. This time there’s no interpolation. Each pattern has \N{CAT FACE} directly in the operator:

use v5.10;

my $double = qr/\N{CAT FACE}/;
say "Double: $double";

my $single = qr'\N{CAT FACE}';
say "Single: $single";

Before v5.30, this was a compile-time error:

$ perl5.28.0 test.pl
\N{NAME} must be resolved by the lexer in regex

Starting with v5.30.0, it works out. Notice that interpolating it directly translates to its code number, while the single-quoted version hasn’t done that step yet:

$ perl5.30.0 test.pl
Double: (?^u:\N{U+1F431})
Single: (?^u:\N{CAT FACE})
Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]