in reply to Re^2: Extracting Data from a second line
in thread Extracting Data from a second line

If I'm working with a regular filehandle, I always use -s to get the length to read, but unfortunately that doesn't work here. -s DATA will give you the length of the entire file, Perl code included, not just the part after __DATA__.

Update: With that stated, I do realize it wouldn't hurt anything to supply a bigger number than required as the third argument to read.

Replies are listed 'Best First'.
Re^4: Extracting Data from a second line
by ikegami (Patriarch) on Feb 16, 2006 at 18:54 UTC

    -s FILE is often too big — not just when using DATA — since read is to be given a number of characters (as opposed to a number of bytes) when binmode isn't set to raw.

    open(FILE, $0) or die("Unable to open $0: $!\n"); $read = read(FILE, $buf='', -s FILE); print("Character mode:\n"); print("Bytes requested: ", -s FILE, "\n"); # 580 print("Chars read: ", $read, "\n"); # 560 print("Chars read: ", length($buf), "\n"); # 560 seek(FILE, 0, 0); binmode(FILE); print("\n"); $read = read(FILE, $buf='', -s FILE); print("Bin mode:\n"); print("Bytes requested: ", -s FILE, "\n"); # 580 print("Chars read: ", $read, "\n"); # 580 print("Chars read: ", length($buf), "\n"); # 580

    But like you said, it's ok if it's too big.

        The number returned by that program is still larger than it needs to be in Windows. There are 41 characters to read in Windows, but $size_of_DATA is 44.