Elizabeth Cortell wrote:
I have a question regarding use of a package's DATA filehandle under
mod_perl.

I keep bits of HTML in the __DATA__ area of my modules to keep them
uncluttered.  I began to notice that for a module loaded at server startup
and kept alive (because it was a content handler), I had no problems
reading DATA and keeping the data.  However, objects created and destroyed
with each request showed erratic behavior; after a few successful reads
from DATA, it began to fail.

I figured that it reads DATA in the first child process, but fails in the
second and thereafter because the file pointer is at the end of __DATA__.
I worked around this by storing DATA's position in a package-level global
variable:

$pos = tell DATA unless $pos; { local $/; seek DATA, $pos, 0; $data =
<DATA>; }


Is my analysis correct and is there a better solution?

yes, it is correct. __DATA__ is just a filehandle, so if you move the fh pointer you need to restore it if you want to re-use it in a persistent environment. Your solution is the only one that I know of.
We ought to document it. Though I'd separate the reset and the use of it:


# reset DATA fh
$pos ||= tell DATA;
seek DATA, $pos, 0;
...
# use it...
$data = <DATA>;

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html



Reply via email to