In perldata it mentions that __END__ behaves just like __DATA__ in top level scripts. So it's pretty easy to put an __END__ in a script with a bunch of junk after it, and then print "Byte offset: ", tell(DATA), "\n"; in the script prior to the __END__ tag. If you do that, you'll see that the DATA filehandle is sitting at the offset to the first line after the __END__ tag.
Now whether or not Perl is doing trickery behind the scenes that we don't know about, I can't say. It's possible that it's keeping track of where the __END__ was seen but continuing to read through to the end of the file anyway, and then seeking back to where __END__ was found. But I can't think of any reason why it would go to all that trouble. That's probably not happening; the simplest explanation, that it isn't reading the entire file, is probably correct.
Here's an example:
#!/usr/bin/env perl
my $string = do {
open my $fh, '<', $0 or die "Cannot open $0: $!\n";
local $/ = undef;
<$fh>;
};
if ($string =~ m/^__END__\n/gm) {
print '__END__ tag found at ', pos($string), "\n";
}
print 'Entire file length: ', length($string), "\n";
END { print 'DATA filehandle tell offset: ', tell(DATA), "\n"; }
__END__
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
This produces the following output:
__END__ tag found at 351
Entire file length: 1892
DATA filehandle tell offset: 351
That seems to indicate that Perl stops reading after it has finished reading the line that contains the __END__.
|