Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: Stupid question about strings...

by AnomalousMonk (Archbishop)
on Jul 14, 2014 at 23:48 UTC ( [id://1093633]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Stupid question about strings...
in thread Stupid question about strings...

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1093633]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-18 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found