Storing Files Inside Your Program Text

Problem

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.

Solution

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

Discussion

__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 ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.