Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

__DATA__ in a Script

by mvaline (Friar)
on Dec 21, 2001 at 19:59 UTC ( [id://133784]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://133784]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found