| Previous | Next
OLE AutomationContents:Creating Objects
The Win32::OLE modules give Perl support for OLE automation. OLE automation is a Microsoft technology based on COM that allows objects created by another application to be used and manipulated by a program through a common interface. The application (or DLL) that implements the automation interface is called the automation server. The application that creates and uses the interface is called the automation controller or automation client. Many popular applications expose their objects through automation. Microsoft Word, Excel, and other Office applications can be used as automation servers. Automation is widely used by Active Server Pages (ASP) and CGI scripts to access data repositories, perhaps via ActiveX Data Objects (ADO). You can even use automation to control many development environments and editors. To create an automation object, the server needs to be registered on the system. This is typically done by the server's installation program, but can be done manually using a utility such as regsvr32.exe. This involves adding entries to the system registry to tell COM how to find the component, the types of interfaces it provides, the type of server it is, etc. You should be able to find the object model, available methods, and properties of the interface in the documentation provided by the application. This object model can be used via Perl's object syntax to create and control objects in your programs. Four modules provide automation functionality to Perl:
Note that there are a few limitations to Win32::OLE. There is currently no support for OCXs or OLE events (notifications generated by the automation server). Win32::OLE implements the Creating ObjectsAutomation objects are represented in Perl as instances of Win32::OLE objects. The module provides three constructors for creating objects from a registered automation server.
Win32::OLE->new(progid, [destructor]) Creates a new automation object. This method always creates a new instance of the server, even if a previous instance of the server is running. If the object cannot be created,
The second, optional argument to the Here is what
Notice that we're supplying Excel.Application as the ProgID. Excel supports several different automation objects, including an Application object, WorkTutorial objects, and several more. You don't necessarily have to create the top-level object (Application, in this case) when dealing with automation objects (this is determined by the automation server). In Excel's case, we could have directly created a WorkSheet object (e.g., Excel.Sheet) or a Chart object, for example.
Win32::OLE->GetActiveObject(progid) Creates an object for a currently active instance of a server, if one exists. If the server is registered, but no instance of it is running, the method returns You should probably call
Win32::OLE->GetObject(filename) Creates an automation object based on a document. $doc = 'c:\test\test.xls'; $x1 = Win32::OLE->GetObject($doc); This code creates an Excel instance based on an Excel file. It is not always clear what type of object |