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.


In reply to Unexpected result using tell/seek within the __DATA__ file by Gulliver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.