in reply to __DATA__ only one time

I must disagree with all of the other responses so far. Why read from the pseudo-filehandle every time, since you need to muck about with seek and tell? Just read everything in once at startup:

my $data; BEGIN { $data = do { local $/; <DATA> }; }

Update: Please heed tye's advice below and use this more as a guide, not as a literal code recommendation. :)

Replies are listed 'Best First'.
Re^2: __DATA__ only one time (close !BEGIN)
by tye (Sage) on Nov 24, 2003 at 19:35 UTC

    Please also close DATA at the same time so that you aren't wasting one of my few available file handles by keeping one open in your module that you will never use again.

    Also note that you can't access DATA inside of a BEGIN block because the compiler hasn't seen the __DATA__ token yet and so doesn't know what offset to let you start reading from and so doesn't make a DATA handle available yet.

    my $data; { local $/; $data = <DATA>; close DATA; }

    You might be able to turn that block into an INIT or a CHECK block, but I've never had a need for such so I won't try to make a recommendation based on ignorance.

                    - tye
Re: Re: __DATA__ only one time
by allolex (Curate) on Nov 24, 2003 at 18:08 UTC

    I was thinking this at the top of the page and by the time I scrolled down to the very bottom, I saw your node. Good thing I read that far :)

    It seems like this is not only easier to code, but more efficient as well... but I have no data, so I'll shut up now.

    --
    Allolex