Think about Loose Coupling | |
PerlMonks |
Item Description: "Where Perl magic meets Windows"
Review Synopsis: Win32::OLE brings you the ability to control many win32 applications
The module lets you create perl objects that act as proxies for the application and it's components in your script. In the example below, $word is your perl proxy connected to a running instance of Word. You can call Word's Automation methods as perl methods on this object, and access the Word properties as hash elements in your perl object.
Automation-friendly Win32 applications expose hierarchies of objects and collections. The module ties these to Perl hashes and arrays for you. Your script just has to navigate the hierarchy and invoke the methods and properties as needed.
How do you know what methods and properties these objects support? Well, you RTFM that comes with the applications, and you use Object browsers that display a minimal documentation extracted from objects themselves (actually from their type libraries). The Win32::OLE module comes with Browser.html, a client-side dynamic html page. The perl code embedded in this page uses Win32::OLE to extract information from type libraries, and displays it in the html browser (IE required).
A short example will illustrate
#! perl -w use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; ### open Word application and add an empty document ### (will die if Word not installed on your machine) my $word = Win32::OLE->new('Word.Application', 'Quit') or die; $word->{Visible} = 1; my $doc = $word->Documents->Add(); my $range = $doc->{Content}; ### insert some text into the document $range->{Text} = 'Hello World from Monastery.'; $range->InsertParagraphAfter(); $range->InsertAfter('Bye for now.'); ### read text from the document and print to the console my $paras = $doc->Paragraphs; foreach my $para (in $paras) { print ">> " . $para->Range->{Text}; } ### close the document and the application $doc->SaveAs(FileName => "c:\\temp\\temp.txt", FileFormat => wdFor +matDocument); $doc->Close(); $word->Quit();
|
---|
Replies are listed 'Best First'. | |
---|---|
RE (tilly) 1: Win32::OLE
by tilly (Archbishop) on Sep 28, 2000 at 04:46 UTC | |
by Anonymous Monk on Mar 01, 2004 at 18:52 UTC | |
by tilly (Archbishop) on Mar 01, 2004 at 23:24 UTC | |
by hopkinsj (Novice) on Dec 15, 2004 at 04:11 UTC | |
by ff (Hermit) on Feb 23, 2005 at 17:18 UTC | |
| |
Re: Win32::OLE-Browser
by sekitan (Beadle) on Mar 15, 2005 at 20:05 UTC | |
by Rudif (Hermit) on Apr 10, 2005 at 21:45 UTC |