You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Issac Goldstand <ma...@beamartyr.net> on 2003/07/20 01:11:45 UTC

[mp2] BEGIN blocks

I seem to be having some trouble that I think may be associated with BEGIN
blocks.  To make sure I'm not jumping at shadows, could someone tell me what
the expected behavior of BEGIN blocks in use()d modules is?  How many times
are they called (during server startup and/or restarts of any type) and
when?

Thanks,
  Issac



Re: [mp2] BEGIN blocks

Posted by Issac Goldstand <ma...@beamartyr.net>.
I'm not really sure what's going on...

I tried making a test module to see what was going on inside, which appeared
to function correctly.  However, the "real" module is still having troubles.
The database handle is opened ($dbh=DBI->connect...) in a BEGIN block in the
"real" module, but the connections don't seem to work when the server
originally loads, until I make a change in the file and Apache::Reload
reloads it (after which it works fine as long as the server's alive).

Here's the module and test results :

package MyTest;

use strict;
use warnings;
use Apache2;
use Apache::Const qw(-compile OK);
use carp qw(cluck);
#use vars qw($VAR);
our $VAR;
BEGIN {
    warn ("Calling BEGIN block");
    warn ("BEGIN: VAR is $VAR");
    $VAR="HELLO";
    warn ("BEGIN: CHANGED VAR to $VAR");
}

warn ("Interpreting module");
warn ("VAR is $VAR");

sub handler {
    my $r=shift;
    $r->content_type('text/plain');
    $r->print("VAR is $VAR");
    return Apache::OK;
}

END {
    warn ("Calling END block");
    warn ("VAR is $VAR");
}

1;

(httpd.conf)
PerlModule MyTest;
<Location /TEST>
  SetHandler perl-script
  PerlResponseHandler MyTest
</Location>

(error_log)
[Thu Jul 24 11:42:01 2003] [notice] Parent: Created child process 2896
calling begin at f:/you/web//YOU/Objects.pm line 17.
Calling BEGIN block at f:/you/web//MyTest.pm line 11.
Use of uninitialized value in concatenation (.) or string at
f:/you/web//MyTest.pm line 12.
BEGIN: VAR is  at f:/you/web//MyTest.pm line 12.
BEGIN: CHANGED VAR to HELLO at f:/you/web//MyTest.pm line 14.
Interpreting module at f:/you/web//MyTest.pm line 17.
VAR is HELLO at f:/you/web//MyTest.pm line 18.
[Thu Jul 24 11:42:07 2003] [notice] Child 2896: Child process is running
[Thu Jul 24 11:42:07 2003] [notice] Child 2896: Acquired the start mutex.
[Thu Jul 24 11:42:07 2003] [notice] Child 2896: Starting 250 worker threads.

(the you/objects.pm is the "real" module giving me trouble)

(output of http://localhost/TEST)
VAR is HELLOHere's the relevant parts (I hope) of my code (full code
available if needed):package YOU::ApacheApp;...use warnings;use CGI;use
DBI;...use YOU::Objects;...    my $event=YOU::Event->new($i); #Should
connect to DB using $dbh......    unless ($event->valid) {print "ERROR
ACCESSING DB! $DB::Objects::VERSION
${YOU::ApacheApp::VERSION}/${YOU::Objects::VERSION}<BR />"; my
$dump=Dumper($event);$dump=~s/ /\&nbsp;/mg;$dump=~s/\n/\<BR \/\>/mg;print
$dump}# $event->valid returns true if connected to DB(Objects.pm)package
YOU::Objects;use strict;use warnings;use DBI;use DB::Objects;use vars
qw($VERSION);our $dbh;BEGIN {    $VERSION=0.01;    my
$mysqldsn="DBI:mysql:database=...;host=...;mysql_compression=1";
$dbh=DBI->connect($mysqldsn,"...","...") or die "couldn't connect:
".DBI->errstr;}END {    $dbh->disconnect();}...In this example, I get the
error message until Apache::Reload reloads the module, after which it works
fine.Any insight would be appreciated.  Issac----- Original Message ----- 
From: "Stas Bekman" <st...@stason.org>
To: "Issac Goldstand" <ma...@beamartyr.net>
Cc: <mo...@perl.apache.org>
Sent: Sunday, July 20, 2003 10:21 PM
Subject: Re: [mp2] BEGIN blocks


> Issac Goldstand wrote:
> > I seem to be having some trouble that I think may be associated with
BEGIN
> > blocks.  To make sure I'm not jumping at shadows, could someone tell me
what
> > the expected behavior of BEGIN blocks in use()d modules is?  How many
times
> > are they called (during server startup and/or restarts of any type) and
> > when?
>
> This is not documented yet for 2.0. I think it should be similar to 1.0.
BEGIN
> blocks are called when the module is loaded. What unusual behavior do you
see?
>
>
> __________________________________________________________________
> Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
> http://stason.org/     mod_perl Guide ---> http://perl.apache.org
> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> http://modperlbook.org http://apache.org   http://ticketmaster.com
>



Re: [mp2] BEGIN blocks

Posted by Stas Bekman <st...@stason.org>.
Issac Goldstand wrote:
> I seem to be having some trouble that I think may be associated with BEGIN
> blocks.  To make sure I'm not jumping at shadows, could someone tell me what
> the expected behavior of BEGIN blocks in use()d modules is?  How many times
> are they called (during server startup and/or restarts of any type) and
> when?

This is not documented yet for 2.0. I think it should be similar to 1.0. BEGIN 
blocks are called when the module is loaded. What unusual behavior do you see?


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