I am defining regexes inside a script, using qr, and pushing them onto an array. But now it appears that if I do not put the regex inside double quotes, the action of pushing it onto the array changes it. Example:
#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper qw(Dumper);
use Data::Dumper::Concise;
my @regexes;
my $rgx = 'dog'; my $mdf = 'i';
$rgx = join ( '', '(?', $mdf, ')', $rgx ) if ($mdf); # in production, $mdf could be empty
eval { $rgx = qr/$rgx/ };
if ($@) # catch illegitimate regex modifier, such as 'g'
{
die "rgx==$rgx; mdf==$mdf; qr throws an error";
}
push @regexes, $rgx;
push @regexes, "$rgx";
print "first try just printing \$rgx\n";
print " no double quotes:";
print $rgx; print "\n";
print "yes double quotes:";
print "$rgx"; print "\n";
print "but now see what happens when I push it onto an array\n";
print Dumper \@regexes;
What this produces:
first try just printing $rgx
no double quotes:(?^:(?i)dog)
yes double quotes:(?^:(?i)dog)
but now see what happens when I push it onto an array
[
qr/(?i)dog/i,
"(?^:(?i)dog)"
]
I thought that (?^:(?i)dog) was a finished product, ready for a regex match, such as
if ( /$rgx/ )
and, in fact, that is why I run the prospective regex through qr.
Why does push change it?
And why does it produce the particular syntax,
qr/(?i)dog/i?