Win8 Strawberry 5.8.9.5 (32) Thu 04/15/2021 1:56:15
C:\@Work\Perl\monks
>perl
use strict;
use warnings;
use Test::More 'tests' => 2;
1..2
use Test::NoWarnings;
my $rqr = qr/\.(?:txt|png)$/i; # regex as Regexp object
my $rsq = '(?i)\.(?:txt|png)$'; # regex as single-quoted string
my @Inputs = qw(
x.txt .txt x.png .png x.TxT .tXt x.pNg .PnG
txt png .txtx .pngx foo xtxtx
);
my @mqr = grep { $_ =~ $rqr } @Inputs; # matches from qr regex
my @msq = grep { $_ =~ $rsq } @Inputs; # matches from '' regex
print "qr matches: @mqr \n";
is_deeply \@mqr, \@msq, 'qr// vs q//';
^Z
qr matches: x.txt .txt x.png .png x.TxT .tXt x.pNg .PnG
ok 1 - qr// vs q//
ok 2 - no warnings
There is no qr// expression for which an equivalent
q// expression cannot (fairly easily) be written.
That said, it's always best IMHO to write regexes as qr//
expressions. Doing so avoids many subtle (and a few blatant) bugs
having to do with differences in escaping and interpolation between
single/double-quoted string constructors and the qr//
constructor, and quantification is better-behaved:
Win8 Strawberry 5.8.9.5 (32) Thu 04/15/2021 2:32:48
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings
my $rqr = qr/foo/;
my $rsq = q/foo/;
my $s = 'foofoofoo';
print "$rqr match: '$1' \n" if $s =~ m{ ($rqr+) }xms;
print "'$rsq' match: '$1' \n" if $s =~ m{ ($rsq+) }xms;
^Z
(?-xism:foo) match: 'foofoofoo'
'foo' match: 'foo'
I find it a neat syntactic feature that in $rqr? $rqr*
$rqr+ $rqr{n}
$rqr{n,m} expressions,
the Regexp object is quantified atomically. This allows
regexes to be factored in a reasonable way.
(It should be noted that split has a number of special-case
interpretations of its PATTERN argument
(update: and some of these differentiate between
string and Regexp representations).)
Give a man a fish: <%-{-{-{-<
|