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

Perl and Word have been playing well together for me for a while now on a W2K/ActiveState 5.8.4 platform. A new wrinkle has come up and I'm trying to verify what is in the Word objects I am creating.

I'd expect to be able to examine those objects with Dumper, but when I do so, I seem to get stuck in a loop and the Windows Task Manager shows the VM Size of my perl.exe pgm getting larger and larger at which point I kill the perl.exe process.

The following sample code extracts the essence of my Dumper test. My question is, "Aren't I able to look at these objects with Dumper?" (The if 0 and if 1 thingies give me easy-to-find debugging hooks to play with.)

(For what it's worth, the original problem I'm investigating is that something new/weird is happening in the Save process. And I don't think I did anything and I'm wondering if something has changed on the OLE side. I can see the Word document being created but when the document is saved to disk, it appears empty. But that's another story....)

use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Data::Dumper; # Update: The following line, per advice below, solves my problem: # $Data::Dumper::Maxdepth=2; # check if Word exists my $x = Win32::OLE->GetActiveObject('Word.Application'); die "Word not Installed" if $@; warn "1 OK to here.\n"; # start Word program, die if unable to unless (defined $x) { unless ( $x = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; }) ) { die "Cannot start Word.\n"; } } warn "2 OK to here.\n"; do_dumper( 'wobjX:', $x ) if 1; warn "3 OK to here.\n"; my $template_file = 'e:\\aa\\myfile.dot'; my $d; unless ( $d = $x->Documents->Add( $template_file ) ) { die "Cannot open file '$template_file': $!\n"; } do_dumper( 'wobjD:', $d ) if 0; undef $x; undef $d; warn "4 OK to here.\n"; sub do_dumper { my ( $label, $variable ) = @_; my $msg = Dumper( $variable ); open P2W, ">> p2w_out.txt"; print P2W "$label:\n$msg\n"; #warn "$label:\n$msg\n"; close P2W; }

Update: Made some minor revisions to sample code to fix cosmetic problems with quoting. Thanks for the advice--I'm getting stuff I can read now. :-)

Replies are listed 'Best First'.
Re: Win32::OLE Word objects and Dumper
by dimar (Curate) on Jan 24, 2005 at 06:12 UTC

    Not being an MSFT OLE expert precludes an exceptionally illuminating answer to your specific inquiry, at least from this monk. Nevertheless, if you have a copy of Word with VBA support, Visual Studio or any of the other MSFT development environments that allow you to set a breakpoint, you can visually scan OLE objects in the graphical debugger that comes with those tools.

    If you haven't tried this yet, you might want to, because you are likely in for a surprise. Specifically, you will notice that certain OLE variables contain multiple nested references that quite literally recurse indefinitely, or cross-reference structures and objects that you may not expect. Just take a look in the MSFT debugger and this will become immediately apparent.

    What's probably happening is Data::Dumper is dutifully doing what you ask of it, and attempting to return a dumpified version of your entire heap as far as Win OLE sees it.

      Specifically, you will notice that certain OLE variables contain multiple nested references that quite literally recurse indefinitely, or cross-reference structures and objects that you may not expect.
      Very true. But there is a way around that. You can set the $Data::Dumper::Maxdepth-variable to a numeric value >0, so that Dumper() will not recurse infinitly.
      use Data::Dumper; $Data::Dumper::Maxdepth=3;

      holli, regexed monk