Storing Files Inside Your Program Text


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
# your data goes here

Similarly, use __END__ within the main program file:

while (<main::DATA>) {
    # process the line
# 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 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.