On May 21, 2009, at 8:36 PM, Steve Revilak wrote:
A few days ago, I was helping a co-worker debug a Perl program.  In
the process of doing this, we found a bit of behavior that seemed
odd.  Here's a minimal program to reproduce what we saw.

m// only sometimes means "match the empty pattern". Depending on context, it can also mean "match the same pattern again".


 --------- f1.pl -------------------------
 use strict;
 use warnings;

 my $line = "hello world\r";
 print "\t<" . ("tri state airport" =~ m//) . ">\n";
 $line =~ s/\r//gs;
 print "\t<" . ("tri state airport" =~ m//) . ">\n";
 exit 0;
 -----------------------------------------

The first m// has no previous pattern to reuse, so works as "match the empty pattern". The second does have a previous pattern, from the substitution, so works as m/\r/. "tri state airport" lacks carriage returns, so doesn't match.

 --------- f2.pl -------------------------
 use strict;
 use warnings;

 my $line = "hello world\r";
 print "\t<" . ("tri state airport" =~ m//) . ">\n";
 $line =~ tr/\r//d;
 print "\t<" . ("tri state airport" =~ m//) . ">\n";
 exit 0;
 -----------------------------------------

This time, there is never a "previous pattern", since tr does translations, not matching. So both mean "match empty pattern".

Or read what Uri posted before I finished typing this.

--kag


_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to