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

Is is manditory to have all the __DATA__ at the bottom of the script.

if it can be placed in the middle of script, how to differentiate the "__DATA__ content" form the program script?

Can someone describe/elobrate/comment about the __DATA__ usage, its constraints and best practices..

Thanks in advance

Anandatirtha

Replies are listed 'Best First'.
Re: reading the __DATA__
by joe++ (Friar) on Nov 14, 2002 at 10:34 UTC
    From perldoc perldata I seem to understand that __DATA__ can only occur at the end of your script:

    The tokens __END__ and __DATA__ may be used to indicate the logical end of the script before the actual end of file. Any following text is ignored, but may be read via a DATA filehandle: main::DATA for __END__, or PACK- NAME::DATA (where PACKNAME is the current package) for __DATA__. The two control characters ^D and ^Z are syn- onyms for __END__ (or __DATA__ in a module). See Self- Loader for more description of __DATA__, and an example of its use. Note that you cannot read from the DATA filehan- dle in a BEGIN block: the BEGIN block is executed as soon as it is seen (during compilation), at which point the corresponding __DATA__ (or __END__) token has not yet been seen.
    You could consider to eval join('', <DATA>); at runtime, which may be useful for obfuscating and to avoid compile time warnings ;-)

    Update: fixed typo.

    --
    Cheers, Joe

Re: reading the __DATA__
by bart (Canon) on Nov 14, 2002 at 11:45 UTC
    "__DATA__", or its synonym(1) "__END__" , terminates the parsing of the rest of the script file, if it's found on a line of its own. So it's always at the end of the script, because it says that it is the end of the script!

    The only exception can be where you have such strings in a literal multiline string, either quoted or in a here-doc; or when used as a here-doc delimiter. Examples:

    #!/usr/bin/perl -w print <<'__END__'; This is a here-doc __DATA__ But it's not over yet! __END__ print "This really is the __END__ "; __DATA__ print "See?\n";
    resulting in:
    This is a here-doc __DATA__ But it's not over yet! This really is the __END__
    I hope this example makes it a bit clear?

    (1) Yes it is legal to do:

    print <DATA>; __END__ one two
    Works, too.