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.

  --------- 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 output consists of two lines, where the result of 

  ("tri state airport" =~ m//)

is placed within angle brackets.  Each print statement gives a
different answer.

   $ perl f1.pl
          <1>
          <>

The first line indicates that "tri state airport" matches the empty
string (which I agree with); the second line indicates that "tri state
airport" does not match the empty string.

With a slight alteration (changing s/// to tr///), we get a different
result.  

  --------- 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;
  -----------------------------------------

   $ perl f2.pl
          <1>
          <1>

(Here, "tri state airport" matches the empty string both times.)

Is s/// supposed to behave this way?

This behavior appears in Perl 5.8.6 (Mac OS X 10.4.11) and Perl 5.8.8
(Mac OS X 10.5.6, Centos 5.2, and Ubuntu lenny).

Steve


Attachment: pgpYccLTN6T9F.pgp
Description: PGP signature

_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to