Mittwoch, 17. Juni 2009

Perl Gotcha I

I'm trying to write some interesting, sometimes unexpected, things one can experience with Perl in the next time.
They are partly based on a talk I did at the German Perl Workshop 2008, see my talk in german.


How does the regex
  m/foo$\nbar/m
gets interpreted?


At first it looks like it matches "foo", then end of line with the $, then a newline with \n and finally "bar".
Let' see:
  my $string = "foo\nbar";
print $string =~/foo$\nbar/m ? 'match':'no match'
Surprisingly the regex does not match like expected.


Explanation:
The regex is interpreted as
  /foo $\ nbar/mx
e.g. it matches "foo", then the content of the special variable $\ (usually undef) and finally "nbar"

Keine Kommentare:

Kommentar veröffentlichen