C:\>perl -E "my $str1 = qq(|L|D|); my $str2 = qr(\|L\|);if ($str2 =~ $ +str1) { say 'foo';}" foo
So why did the code I showed return true (or "foo")?"
Because the string defining the regex used in the $str2 =~ $str1 expression (i.e., $str1) has the empty pattern as two (!) of its alternatives, and the empty pattern matches everything.
my $str1 = qq(|L|D|); has the empty pattern twice. It doesn't really matter what else is present. The stringization of the output of the qr(\|L\|) expression is a bit complex, but it could be anything.
c:\@Work\Perl>perl -wMstrict -le "my $str1 = qq(|L|D|); my $str2 = qr(\|L\|); print qq{stringization of qr// output: '$str2'}; if ($str2 =~ $str1) { print 'foo'; } " stringization of qr// output: '(?^:\|L\|)' foo c:\@Work\Perl>perl -wMstrict -le "my $str1 = qq(|X|Y|); my $str2 = qr(\|L\|); if ($str2 =~ $str1) { print 'foo'; } " foo c:\@Work\Perl>perl -wMstrict -le "my $str1 = qq(|X|Y|); my $str2 = qq(aaaaa); if ($str2 =~ $str1) { print 'foo'; } " foo
Update: Changed my $str2 = qq(xyzzy); in third code example above to qq(aaaaa) to eliminate any question of case-insensitive matching.
In reply to Re^5: Stupid question about strings...
by AnomalousMonk
in thread Stupid question about strings...
by kepler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |