Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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

by Anonyrnous Monk (Hermit)
on Feb 09, 2011 at 19:37 UTC ( #887260=note: print w/replies, xml ) Need Help??

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

But how to use multiple sets of "__DATA__" still stands.

The answer is simple: you can't. There's only one __DATA__ section. At least in the main package.

In other words, you'd have to use some separator, e.g. an empty line (if that doesn't occur otherwise in the data), and then split it up into sets yourself.

You could in theory put different __DATA__ sections in different modules (i.e. different packages in different files), and then access them as Foo::DATA, etc., but you might as well just put the data sets in normal files and read them from there...

# File package Set1; 1; __DATA__ foo1 foo2 foo3 # File package Set2; 1; __DATA__ bar1 bar2 bar3 # main script #!/usr/bin/perl -w use strict; use Set1; use Set2; print "Set 1:\n"; while (<Set1::DATA>) { print } print "Set 2:\n"; while (<Set2::DATA>) { print }


Set 1: foo1 foo2 foo3 Set 2: bar1 bar2 bar3

Replies are listed 'Best First'.
Re^4: How to use __DATA__ efficiently, help!
by belden (Friar) on Feb 10, 2011 at 16:30 UTC
    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.

      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:

      $ ./ set 1 ... __DATA__ set 2 ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://887260]
[Corion]: A good daystart to everybody!
[GrandFather]: It's a good day end here Corion. The start was somewhat less than average!
[Corion]: GrandFather: All's well that ends well? ;)
[GrandFather]: I'm fighting with a third party device our software is to support. The documentation for the device's SDK is quite a lot less than average and most of today was spent ...
[GrandFather]: discovering that one of the sensors for the device lies about the gain range it is using!
[GrandFather]: However, by the end of the day I had discovered its deceptions and now have it working correctly, so yes, all's well that ends well. :-D

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-08-24 07:04 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (365 votes). Check out past polls.