You have data that you want to bundle with your program and treat as though it were in a file, but you don't want it to be in a different file.
Use the __DATA__
or __END__
tokens after your program code to mark the start of a data block, which can be read inside your program or module from the DATA
filehandle.
Use __DATA__
within a module:
while (<DATA>) { # process the line } __DATA__ # your data goes here
Similarly, use __END__
within the main program file:
while (<main::DATA>) { # process the line } __END__ # your data goes here
__DATA__
and __END__
indicate the logical end of a module or script before the physical end of 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 inaccessible.
This lets you write self-contained programs that would ordinarily keep data kept in separate files. Often this is used for documentation. Sometimes it's configuration data or old test data that the program was originally developed with, left lying about in case it ever needs to be recreated.
Another trick is to use DATA
to find out the current program's or module's size or last modification date. On most systems, the $0
variable will contain the full pathname to your running script. On systems where $0
is not correct, you could try the DATA filehandle instead. This can be used to pull in the size, modification date, etc. Put a special token __DATA__
at the end of the file (and maybe a warning not to delete it), and the DATA filehandle will be to the script itself.
use POSIX qw(strftime);
$raw_time = (stat(DATA))[9];
$size = -s DATA;
$kilosize = int($size / 1024) . 'k';
print "<P>Script size is $kilosize\n";
print strftime("<P>Last script update: %c (%Z)\n", localtime($raw_time));
__DATA__
DO NOT REMOVE THE PRECEDING LINE.
Everything else in this file will be ignored.
The "Scalar Value Constructors" section of perldata (1), and the "Other literal tokens" section of Chapter 2 of Programming Perl