[kw-pm] __END__ and __DATA

Cees Hek ceeshek at gmail.com
Tue Aug 15 08:16:57 PDT 2006


On 8/15/06, Robert Pike <roberthpike at yahoo.com> wrote:
> 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.

I don't have any thorough notes, but here are some quick points:

- You only need one of __END__ or __DATA__, as they both serve the same purpose
- for placement, it has to appear at the end of the file
- You can only have one __DATA__ section in your file (if you really
want more, you can look at Damian's Inline::File module, but I would
avoid it at all costs)
- Be careful using __DATA__ in a program that forks as you will
effectively be forking a process with an open filehandle


I only ever use __DATA__ sections in simple little scripts, sometimes
in example scripts that I post to mailing lists (being able to post
one file is much easier than needing the user to save multiple files
and worry about paths and such things).  One example being when
highlighting a feature in Template Toolkit:


use Template;

my $template = Template->new( POST_CHOMP => 1 );
my $data = {
    one => [qw(a b c d e)],
    two => [qw(f g h i j)],
};
$template->process(\*DATA, $data) or die $template->error;

__DATA__
[% FOREACH letter IN one %]
[% letter %][% UNLESS loop.last %], [% END %]
[% END %]

[% two.join(', ') %]


Hope that helps somewhat...

Cheers,

Cees


More information about the kw-pm mailing list