Gulliver has asked for the wisdom of the Perl Monks concerning the following question:
This is confusing: What I'm seeing is that after the __DATA__ statement the tell function counts '\n' as a single character but before __DATA__ the newline counts as two. If seek() followed the same rule then everything would be ok but it doesn't seem to. When I seek and print within DATA it prints partial lines as can be seen in the output below.
#!/usr/bin/perl use strict; use warnings; my @DATA_FH_ARR = (tell DATA); my $i=1; while (<DATA>){ if (/__DATA__/) { $DATA_FH_ARR[$i++]=tell DATA; } print; } print "\n\@DAT_FH_ARR: @DATA_FH_ARR\n"; my $line; seek DATA, $DATA_FH_ARR[2], 0; $line= <DATA>; print '1:', $line; $line= <DATA>; print '2:', $line; seek DATA, $DATA_FH_ARR[3], 0; $line= <DATA>; print '1:', $line; $line= <DATA>; print '2:', $line; __DATA__ ab __DATA__ ab __DATA__ ab __DATA__ lotsa junk nothing
In the array of DATA file positions the offset was 12 between each. When I added a newline before the third __DATA__ statement the offset changed to 13. But when I added a newline in the code section above the first __DATA__ the offsets shifted by 2. I was expecting the same in both places.
Update: The offsets I'm referring to in the previous paragraph are the offsets shown in the program output. 468 480 493 505 are the offset positions from the array that was created from "tell DATA". By taking the differences between sucessive numbers I could see that adding a newline changed the difference from 12 to 13. 493-480=13; 480-468=12
Here is the output that shows the offsets of 12,13,12
ab __DATA__ ab __DATA__ ab __DATA__ lotsa junk nothing @DAT_FH_ARR: 468 480 493 505 1:A__ 2:ab 1:ATA__ 2:lotsa junk
Here is after I added a newline in the main code. All offsets shifted by 2.
ab __DATA__ ab __DATA__ ab __DATA__ lotsa junk nothing @DAT_FH_ARR: 470 482 495 507 1:A__ 2:ab 1:ATA__ 2:lotsa junk
This is on Win XP with Strawberry Perl 5.12.0.
|
|---|