[kw-pm] __END__ and __DATA

eMaki eric at uc.org
Tue Aug 15 08:13:08 PDT 2006


> Anyone have any thorough and helpful notes/ share
> personal experiences on using __END__ and __DATA__?
> Exact placement of (i.e. any needed trailing lines,
> etc..,), where to use, effects of, best uses for, and
> whether multiple ones can be placed in the same script
> (and, if so, how they are treated). Thanks in advance.

see "Special Literals" in perldata.

Depends what you mean by script.  A file can only have one, but a
program can have many files.  However, the DATA filehandle is a
package variable, not a lexical.  If you have multiple files in
one package, you can't have DATA sections in both, or at least
you shouldn't.

One gotcha is closing DATA - that's up to you.  If you use DATA
sections, then your program has an open filehandle for your
program file, even after Perl would normally release it.  So
normal OS locking applies.  This has bit me a number of times...
starting a long running listener with a __DATA__ section, and not
being able to rename the file, etc.

__END__ is only special in your main script file, everywhere else
it is just a stop-parsing mark.

I don't personally ever use them in production code - I put data
in datafiles.

fishbot


More information about the kw-pm mailing list