Home: Perl Programming Help: mod_perl:
mod_perl with Win32::OLE

New User

Aug 25, 2009, 1:15 PM

Views: 14278
mod_perl with Win32::OLE

Can anybody explain difference between first and second call of the same web page pointed to same mod perl location.
The test was done on a windows xp machine running Apache server version 2.2.11, mod perl version 2.0.4 using perl version 5.10.0

mod perl section in the httpd.conf file is:
LoadFile "c:/Apache2/bin/libapreq2.dll"
LoadModule apreq_module modules/mod_apreq2.so
PerlRequire "C:/Apache2/conf/extra/startup.pl"
<Files *.pl>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
Options +ExecCGI
PerlOptions +ParseHeaders
erlModule Apache2::Hello
<Location /hello>
SetHandler modperl
PerlResponseHandler Apache2::Hello::ResponseH

mod perl startup script startup.pl is:

use Apache::DBI ();
use ModPerl::Util (); #for CORE::GLOBAL::exit
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil ();
use Apache2::ServerRec ();
use Apache2::ServerUtil ();
use Apache2::Connection ();
use Apache2::ConnectionUtil ();
use Apache2::Log ();
use APR::Table ();
use ModPerl::Registry ();
use Apache2::Const -compile => ':common';
use APR::Const -compile => ':common';
use Apache2::Cookie;
use Apache2::Directive ();
use CGI ();
Apache2::ServerUtil->server->push_handlers(PerlChildInitHandler => \&ChildInitH);
Apache2::ServerUtil->server->push_handlers(PerlChildExitHandler => \&ChildExitH);

sub ChildInitH {

require Win32::OLE;
require Win32::OLE::Enum;
my($child_pool, $s) = @_;
$s->log_error("child initialize");
Win32::OLE->Initialize( 2 );

# use existing instance if Word is already running
eval {$wd = Win32::OLE->GetActiveObject('Word.Application')};
unless (defined $wd) {
$wd = Win32::OLE->new('Word.Application', sub {$_[0]->Close;})
or return Apache2::Const::SERVER_ERROR;
return Apache2::Const::OK;

sub ChildExitH {
if (defined $wd) {
Apache2::ServerUtil->server->log_error("child uninitialize");
Win32::OLE->Uninitialize( );
return Apache2::Const::OK;

mod perl script Apache2/Hello.pm is:
package Apache2::Hello;
sub ResponseH {
my $r = shift;
$r->print( "<html><body>dump word document on the mod perl way<br>");
$r->print( "bookmarks:<br>");

my $document;
my $wdoc = "c:\\test\\a.doc";
$document = Win32::OLE ->GetObject($wdoc);
my $bookmarks = $document->Bookmarks();
my $enumerate = new Win32::OLE::Enum($bookmarks);
while(defined(my $bookmark = $enumerate->Next()))
my $name = $bookmark->{Name};
$r->print( "</body></html>");
return Apache2::Const::OK;

cbi-bin perl script wc.pl is:
use strict;
require Win32::OLE;
require Win32::OLE::Enum;

print "Content-type: text/html\n\n";
print "<html><body>dump word document on the cgi way<br>bookmarks:<br>";

Win32::OLE->Initialize(2 );

my $wdoc = "c:\\test\\a.doc";
my $document = Win32::OLE -> GetObject($wdoc);
my $bookmarks = $document->Bookmarks();
my $enumerate = new Win32::OLE::Enum($bookmarks);
while(defined(my $bookmark = $enumerate->Next()))
my $name = $bookmark->{Name};
print "$name<br>";
print "</body></html>"

Both scripts cgi-bin script wc.pl and mod perl script Apache2/Hello.pm list bookmarks from the word document.
When mod perl script is invoked first time after http server started it fails with an error message: "can't locate object method "GetObject" via package "Win32::OLE" "
In the case when cgi-bin address is requested first then mod perl script runs just fine.
I'm wondering why mod perl script fails first time is invoked.
any suggestion