mvaline has asked for the wisdom of the Perl Monks concerning the following question:

I've written a simple script to randomly print out a line from a file (it's a random error generator). I would like to store the contents of that file in the Perl script. I have investigated the use of the __DATA__ construct, but everything I've read discusses using it in a module. Is there any way to use it in a script? If so, how would that be done?

Replies are listed 'Best First'.
Re: __DATA__ in a Script
by dragonchild (Archbishop) on Dec 21, 2001 at 20:02 UTC
    Just read from the <DATA> filehandle. :-) Something along the lines of:
    while (<DATA>) { print "From Data: $_\n"; } __DATA__ This Is Lieutenant Data

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: __DATA__ in a Script
by mirod (Canon) on Dec 21, 2001 at 20:17 UTC

    Note that if you need more than one "DATA" file or if you need to write back to it you can use one of my favorite modules: Inline::Files.

Re: __DATA__ in a Script
by JojoLinkyBob (Scribe) on Dec 21, 2001 at 23:01 UTC
    Same idea, but shorter:
    $count = @lines = <DATA>; print @lines[rand($count)];
    Desert coder