in reply to Perl stops reading __DATA__ when meeting SUB character on Windows

binmode(DATA) would likely solve the problem if you could do it soon enough.

My bet is that the Perl interpreter has already read far enough past __END__ / __DATA__ (reading from disk is done using buffers of several kB, even if only one line is returned to the caller) for the CTRL-Z to have caused EOF to be detected. Throwing in binmode at that point doesn't clear the EOF and you are still left with only being able to read the bytes that have been left in the handle's buffer.

You can work around this by re-opening your sourcecode file and doing binmode() immediately. But it gets tricky to seek to the exact offset for "immediately after __END__ / __DATA__", because reading w/o binmode on Win32 means "\r" characters likely got stripped and so tell is likely to be off by several bytes.

I'd just re-open the source code and read until I found "\n__END__\s*\n" or "\n__DATA__\s*\n" and then read all of the bytes after that. It isn't hard to write Perl code such that you never include "__END__" at the front of any line before the one where you used it to mark the end of your Perl code.

- tye        

  • Comment on Re: Perl stops reading __DATA__ when meeting SUB character on Windows (buffering)
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl stops reading __DATA__ when meeting SUB character on Windows (buffering)
by yfnecz (Novice) on Jan 03, 2014 at 02:33 UTC
    Thanks, I'll try that, probably that'd work. Just wanted to know, maybe there is some other simpler way to work this around :)
Re^2: Perl stops reading __DATA__ when meeting SUB character on Windows (buffering)
by yfnecz (Novice) on Jan 03, 2014 at 10:43 UTC
    By the way, when I try to read/write from file, not from DATA, the same binary content, then after using binmode it all gets read & written. So, using this way should help.
Re^2: Perl stops reading __DATA__ when meeting SUB character on Windows (Ctrl-Z on STDIN)
by Anonymous Monk on Jan 03, 2014 at 13:38 UTC

    I don't think it matters when reading from a file

    If memory serves Ctrl-Z only ever worked on STDIN, as in

    $ perl - use Data::Dump qw/ dd /; while(<DATA>){ dd($_); } __DATA__ a "a\n" asdf^Z "asdf\32\n" ^Z