johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Somehow I ran into an equivalent of the following, for the script:
use strict; while(my $a=<DATA>){ print $a; } __END__ 1 2 3 __DATA__ a b c
I was expecting the result to be:
a b c
But I actually got:
1 2 3 __DATA__ a b c
So __END__ is equivalent to __DATA__? eveything after __END__ or __DATA__ can be accessed via <DATA>?

Replies are listed 'Best First'.
Re: __END__ is equivalent to __DATA__ ?
by Zaxo (Archbishop) on Dec 07, 2004 at 23:22 UTC

    Almost, but not quite equivalent. The data following __END__ is always associated with namespace main::, while __DATA__ is file scoped. That will matter for data sections occuring in modules.

    A data section may also be started with Ctrl-Z (ascii \x1a).

    You can get the effect your intuition wants with the Inline::Files module. It enables more than one labelled section of data at the end of a perl source file.

    After Compline,
    Zaxo

Re: __END__ is equivalent to __DATA__ ?
by mifflin (Curate) on Dec 07, 2004 at 23:11 UTC
    The perl Cookbook recipe 7.6 says they are the same...

    <snippet from the book>

    __DATA__ and __END__ indicate the logical end of a module or script before the physical end of the file is reached. Text after __DATA__ or __END__ can be read through the per-package DATA filehandle. For example, take the hypothetical module Primes. Text after __DATA__ in Primes.pm can be read from the Primes::DATA filehandle.

    __END__ behaves as a synonym for __DATA in the main package. Text after__END__ tokens in modules is inaccessable.

      Text after __DATA__ in Primes.pm can be read from the Primes::DATA filehandle.
      Ahh, too bad I didn't get to review that book... I could have marked that sentence as a technical error. As perldata clearly states:
      Text after __DATA__ but may be read via the filehandle "PACK- NAME::DATA", where "PACKNAME" is the package that was current w +hen the __DATA__ token was encountered.
      So, what matters is not the name of the file, but rather the "current package" at the point just before the __DATA__ token. I'm surprised Nat and Tom made such a straightforward mistake. (I've just submitted this as an errata for the book, so hopefully future books will get it right.)

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Re: __END__ is equivalent to __DATA__ ?
by ccn (Vicar) on Dec 07, 2004 at 23:22 UTC

    see perldata for Special Literals

    For compatibility with older scripts written before __DATA__ was introduced, __END__ behaves like __DATA__ in the toplevel script (but not in files loaded with require or do) and leaves the remaining contents of the file accessible via main::DATA.
Re: __END__ is equivalent to __DATA__ ?
by johnnywang (Priest) on Dec 08, 2004 at 18:28 UTC
    Thank you all, I did some test to confirm all your points. Here are the scripts to document this:
    use strict; use foo; print "From <DATA>:\n"; print <DATA>; print "From <foo::DATA>:\n"; print <foo::DATA>; print "From <bar::DATA>:\n"; print <bar::DATA>; __DATA__ a b c
    Here's the foo.pm:
    package foo; use strict; package bar; 1; __DATA__ 1 2 3 4
    Running the first script gives:
    From <DATA>: a b c From <foo::DATA>: From <bar::DATA>: 1 2 3 4