blahblahblah has asked for the wisdom of the Perl Monks concerning the following question:

I'm in the process of fixing up a large cgi application so that it runs correctly under mod_perl 2.0, using Apache::Registry. There were a handful of END blocks, most of which I've rewritten using the cleanup_register function, which works great. Now I've come to one which prints to STDOUT in an END block, but it's not printing anything since I switched to cleanup_register. It seems like Apache is closing STDOUT (or untying it, or whatever) before it runs the cleanup code.

Anyone know of a different way I could print to STDOUT as the script is finishing, or get STDOUT working in the cleanup code?

The script below is a simple example of what I'm talking about. "finishing stdout." never gets printed. The commented out lines didn't help.

#!/usr/local/bin/perl print "Content-type: text/plain\n\n(in main code)\n"; # my $old; # open ($old, ">&STDOUT") or die "Can't dup STDOUT: $!"; my $r = Apache->request; $r->pool->cleanup_register(\&finish); sub finish { print STDERR "finishing...\n"; # open (STDOUT, ">&", $old) or die "Can't dup \$old: $!"; print "finishing stdout.\n"; print STDERR "finished.\n"; }

Replies are listed 'Best First'.
Re: Can I print to STDOUT in mod_perl cleanup_register code?
by ikegami (Patriarch) on Sep 30, 2004 at 20:41 UTC

    Are you trying to send something to the client? I think $r->pool is only freed at the end of the request, so your handler is being called after the content is already sent. The docs indicate $c->pool->cleanup_register should be used "to run a cleanup at the end of each connection phase":

    use Apache::Connection (); use APR::Pool (); $c->pool->cleanup_register(\&my_cleanup);

    Maybe you'll have better luck if you asked this question on the mod_perl mailing list.