in reply to pattern match against in-memory file causes odd behavior

according to the definition in perlre, \Z and $ should, under all flag settings, be matching everywhere that \z does, so the fact that one is getting matches with \z but not \Z or $, is at best, an undocumented violation of that contract.

Trying this out in my 5.10.1 debugger on (Debian)Linux, I'm getting quite erratic behavior:

  DB<525> open $fh, '>', \$memory
  DB<526> print $fh 'abc';
  DB<527> x $memory =~ m/(.*)\Z/
  empty array
  DB<528> print $fh 'def';
  DB<529> x $memory =~ m/(.*)\Z/
0  'abcdef'
  DB<530> print $fh 'ghi';
  DB<531> x $memory =~ m/(.*)\Z/
  empty array
  DB<532> print $fh 'jkl';
  DB<533> x $memory =~ m/(.*)\Z/
  empty array
  DB<534> print $fh 'mno';
  DB<535> x $memory =~ m/(.*)\Z/
0  'abcdefghijklmno'
which just smells of buggity-bug-bug-bug.
  • Comment on Re: pattern match against in-memory file causes odd behavior

Replies are listed 'Best First'.
Re^2: pattern match against in-memory file causes odd behavior
by Eliya (Vicar) on Dec 28, 2011 at 06:15 UTC

    It seems in some cases the trailing \0 of the PV is missing (according to Devel::Peek), and in those cases, the regex doesn't match:

    use Devel::Peek; open my $fh, '>', \my $memory_file; for (qw(abc def ghi jkl mno)) { print $fh $_; Dump $memory_file; if ( $memory_file =~ m/^(.*)$/ ) { print "==> matched: $1\n" } } __END__ SV = PV(0x75cce8) at 0x783f70 REFCNT = 2 FLAGS = (PADMY,POK,pPOK) PV = 0x7b2e10 "abc"\0 CUR = 3 LEN = 8 ==> matched: abc SV = PV(0x75cce8) at 0x783f70 REFCNT = 2 FLAGS = (PADMY,POK,pPOK) PV = 0x7b2e10 "abcdef"\0 CUR = 6 LEN = 8 ==> matched: abcdef SV = PV(0x75cce8) at 0x783f70 REFCNT = 2 FLAGS = (PADMY,POK,pPOK) PV = 0x7b2e10 "abcdefghi" <--- !! CUR = 9 LEN = 16 SV = PV(0x75cce8) at 0x783f70 REFCNT = 2 FLAGS = (PADMY,POK,pPOK) PV = 0x7b2e10 "abcdefghijkl"\0 CUR = 12 LEN = 16 ==> matched: abcdefghijkl SV = PV(0x75cce8) at 0x783f70 REFCNT = 2 FLAGS = (PADMY,POK,pPOK) PV = 0x7b2e10 "abcdefghijklmno"\0 CUR = 15 LEN = 16 ==> matched: abcdefghijklmno

    Update: some further testing (with minor modifications) shows that there are also cases, where it does match despite a missing trailing \0.   So yes, erratic, at best :)