7.12. Storing a File Inside Your Program Text7.12.1. ProblemYou 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. 7.12.2. SolutionUse 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 7.12.3. DiscussionThe _ _DATA_ _ and _ _END_ _ symbols tell the Perl compiler there's nothing more for it to do in the current file. They represent the logical end for code in a module or a program before the physical end-of-file. 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. Any text occurring after an _ _END_ _ token in a module is completely inaccessible. This lets you write self-contained programs instead of keeping data 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 should it ever be needed again. 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 is available 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. If you want to store more than one file in your program, see Recipe 7.13. 7.12.4. See AlsoThe "Scalar Value Constructors" section of perldata(1), and the "Other Literal Tokens" section of Chapter 2 of Programming Perl; Recipe 7.13 Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|