You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Thomas Wittek <ma...@gedankenkonstrukt.de> on 2006/09/17 13:56:22 UTC

Problems with __DATA__ within a module/handler and mod_perl2 (Own handler - NO CGI script) - Bug?

Hello!

I guess I've found a bug in mod_perl2. But I'd like to ask the users
list first, if anyone has an idea what goes wrong.

I've run into troubles accessing the __DATA__ section of my
modules/handlers. A minimal demonstration handler looks like this:

 #!/usr/bin/perl
 package TestHandler;
 sub handler {
   warn tell DATA;
   warn <DATA>;
   return 0;
 }
 1;
 __DATA__
 foo
 bar
 baz

(On the first run) It will correctly put this into the server log:

 121 at [..]/TestHandler.pm line 5.
 foo
 bar
 baz

But when I preload this module using the Apache directive

 PerlRequire /some/preload_script.pl

which contains:

 use TestHandler;

The cursor on the DATA filehandle will be wrong:

 133 at /usr/local/lib/perl/5.8.7/TestHandler.pm line 5.
 Warning: something's wrong at [..]/TestHandler.pm line 6.

In this case the cursor is positioned at the end of the file (which has
a length of 133 bytes). In other, larger modules the cursor sometimes is
positioned somewhere in the middle of the DATA section.

I don't think that this is the intended behaviour.

It works fine when not preloading the module. It also works fine on
Apache1/mod_perl1.

Any ideas?

Thanks!
-Thomas


Re: Problems with __DATA__ within a module/handler and mod_perl2 (Own handler - NO CGI script) - Bug?

Posted by Thomas Wittek <ma...@gedankenkonstrukt.de>.
Perrin Harkins schrieb:
> Thomas Wittek wrote:
>> The cursor on the DATA filehandle will be wrong:
> 
> I think you just need to rewind it after reading it.  See the docs for
> seek() and tell().  Here's an example:

No, the position is already wrong before any read. So saving it for
later purposes won't fix it.

The bug (?) even occurs in this minimal handler without any reads:

  #!/usr/bin/perl
  package TestHandler;
  sub handler {
    warn tell DATA;
    return 0;
  }
  1;
  __DATA__
  foobarbaz

Correct result without preloading of the module:

  93 at /mnt/data/pro/konstrukt/dev/lib/TestHandler.pm line 4.

Incorrect result when preloading it in a script which is loaded by
Apache's PerlRequire:

  103 at /mnt/data/pro/konstrukt/dev/lib/TestHandler.pm line 4.

The cursor is positioned at the end of the file. On some larger files it
is often positioned somewhere in the middle of the DATA section.

BTW: I forgot to post my configuration details:

  root@server# dpkg -l | pcregrep '(libapache2-mod-perl2 |apache2 )'
  ii  apache2                    2.0.55-4.1 [..]
  ii  libapache2-mod-perl2       2.0.2-2    [..]
  root@server# uname -a
  Linux server.zentrifuge.intra 2.6.17 #1 PREEMPT Tue Jul 11 02:32:04
CEST 2006 i686 GNU/Linux

Best regards

-- 
Thomas Wittek
http://gedankenkonstrukt.de/
Jabber: streawkceur@jabber.i-pobox.net


Re: Problems with __DATA__ within a module/handler and mod_perl2 (Own handler - NO CGI script) - Bug?

Posted by Perrin Harkins <pe...@elem.com>.
Thomas Wittek wrote:
> The cursor on the DATA filehandle will be wrong:
> 
>  133 at /usr/local/lib/perl/5.8.7/TestHandler.pm line 5.
>  Warning: something's wrong at [..]/TestHandler.pm line 6.

I think you just need to rewind it after reading it.  See the docs for 
seek() and tell().  Here's an example:

     my $data_start = tell(DATA);
     while (<DATA>) {
         # do something useful
     }

     seek(DATA, $data_start, 0);
     while (<DATA>) {
         # ...
     }

- Perrin