Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: How to use __DATA__ efficiently, help!

by belden (Friar)
on Feb 10, 2011 at 16:30 UTC ( [id://887495]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How to use __DATA__ efficiently, help!
in thread How to use __DATA__ efficiently, help!

Namespacing your __DATA__s is a clever trick, but you're wrong in saying that __DATA__ can't be accessed twice. You can use __DATA__ twice: just rewind it. You need to do so carefully:
#!/usr/bin/perl use strict; use warnings; my $data_start = tell DATA; print while (<DATA>); seek DATA, $data_start, 0; print while (<DATA>); __DATA__ hello world
If you don't bother to tell DATA; ahead of time and instead just seek DATA, 0, 0;, then you effectively print "hello world\n`cat $0`" which isn't what you're expecting.

Is this the best solution to the original poster's problem? Nope. Is it a solution to "how do I re-process this normal filehandle?" Yep.

Replies are listed 'Best First'.
Re^5: How to use __DATA__ efficiently, help!
by Anonyrnous Monk (Hermit) on Feb 10, 2011 at 17:32 UTC
    but you're wrong in saying that __DATA__ can't be accessed twice

    This is not what I said.  I said that you can only have one __DATA__ section (per package/file), not that you can only read it once.

    The OP asked "how to use multiple sets of __DATA__", which I understood to mean something like this (also see the OP's sample code)

    #!/usr/bin/perl while (<DATA>) { print } __DATA__ set 1 ... __DATA__ set 2 ...

    which obviously acts as only one DATA section:

    $ ./887252.pl set 1 ... __DATA__ set 2 ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found