ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
19.3 Using Automation ObjectsIn this section, we're going to explore automation objects by building a simple progam that sends a message using Microsoft's Active Messaging Library. If you don't have Active Messaging (if you have MAPI, you probably have it) on your system, you can still follow the concepts, which are generally applicable to using automation. Remember, though, that the specific methods, properties, and objects that a server exposes are specific to that server. To learn more about Active Messaging, try the Microsoft MSDN[2] web site at www.microsoft.com/msdn/sdk/ and look for the for Active Messaging Library documentation. The Active Message Library is a complex API that provides complete services for messaging, message stores, providers, transports, and more; but we're just going to touch on the basics of sending a message here.
The first thing we need to do is to create a Active Messaging session. This happens to be the top-level automation object for our purposes, so we'll start here with use OLE; $ActiveSession = CreateObject OLE "MAPI.Session" || die "CreateObject: $!"; The ProgID for the Active Messaging Session object is MAPI.Session, so that's the argument that we give to $LogonName = "Erik Olson"; $LogonPasswd = undef; # use stored one, or prompt die "Logon: $!" if $ActiveSession->Logon($LogonName, $LogonPasswd); # Logon returns 0 on success Here, we're calling the $Message = $ActiveSession->Outbox->Messages->Add(); Now, things are starting to get interesting. We're using the $ActiveSession object to call a method named Now that we have a Message object, we can start doing things with the message. First, we need to add a recipient. This involves another nested automation-object call: $Recipient = $Message->Recipients->Add(); Here we're calling the Recipients method of the message object that returns a recipients object. We then call the Add method of the recipients object to get a Recipient object that we can use. Let's set some properties of the recipient object: $Recipient->{Name} = "Erik Olson"; # to address $Recipient->{Type} = 1; # ugly constant, means this is a To address We've set the
After setting the recipient information, we need to resolve it to a name in the Active Messaging address book. We do this by calling the Resolve member of the Recipient object: $Recipient->Resolve(); Now that we know where our message is going, let's add some data to it. We need at least a subject and a body, both of which are properties of the Message object. $Message->{Subject} = "A Message From Perl"; $Message->{text} = "Perl does automation!"; All that remains is to save the message, send it, and terminate our session: $Message->Update(); $Message->Send(1, 0, 0); $Message->Logoff(); We call the
Let's put everything together: use OLE; $LogonName = "Erik Olson"; # send message to me $LogonPasswd = undef; # use stored passwd $ActiveSession = CreateObject OLE "MAPI.Session" || die "CreateObject: $!"; # create session die "Logon: $!" if $ActiveSession->Logon($LogonName, $LogonPasswd); # logon (returns 0 on success) $Message = $ActiveSession->Outbox->Messages->Add(); $Recipient = $Message->Recipients->Add(); $Recipient->{Name} = "Erik Olson"; # to address # ugly constant, means this is a To address $Recipient->{Type} = 1; $Recipient->Resolve(); # resolve name - hope it's there $Message->{Subject} = "A Message From Perl"; $Message->{text} = "Perl does automation!"; $Message->Update(); # save it $Message->Send(1, 0, 0); # send it - don't show UI $ActiveSession->Logoff(); # end session 19.3.1 Data-Access ObjectsIf you are a Perl programmer looking for a database solution, you owe it to yourself to check out Microsoft's ActiveX Data Objects (ADO), which provide an automation interface to database access. ADO is a powerful data-access layer that you can use from Perl, PerlIS, or PerlScript. This layer is particularly interesting in conjunction with Active Server pages and PerlScript. See www.microsoft.com/ADO/ for more information on ADO. The ActiveState site (www.activestate.com) has several samples using PerlScript and ADO for database access. Just to tempt you, here's a quick example that uses the sample database shipped with the OLEDB SDK (OLE Database Software Development Kit), with which ADO is included. For more information on the OLEDB SDK, see www.microsoft.com/oledb. The sample database contains a table called Employees, which includes the fields LastName, FirstName, and EmployeeID. The following program just opens the data source (you have to have an ODBC driver installed for Microsoft Access database files) and lists all the rows in the Employees table. Regardless of which data source you choose to use ADO with, you'll find the procedure to be similiar. use OLE; $conn = CreateObject OLE "ADODB.Connection" || die "CreateObject: $!"; # create ADO auto object $conn->Open('OLE_DB_NWind_Jet'); # connect to data source $sql = "SELECT * FROM Employees ORDER BY LastName, FirstName"; $rs = $conn->Execute($sql); # grab all records in table while(!$rs->EOF()) { $lastname = $rs->Fields('LastName')->Value; $firstname = $rs->Fields('FirstName')->Value; $empid = $rs->Fields('EmployeeId')->Value; write; # print them out $rs->MoveNext(); } $rs->Close(); # shut down the recordset $conn->Close(); # close the data source # some formats for a quick printout format STDOUT = @<<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< $empid, $firstname, $lastname . format STDOUT_TOP = Page @<< $% ID First Last ===== ==================== ========================= . Our first task is to create the automation object using the now familiar Here's another quick program that inserts an employee into the Employees table: use OLE; $firstname = "Homer"; # hardcode some values to insert $lastname = "Simpson"; $empid = "3001"; $conn = CreateObject OLE "ADODB.Connection" || die "CreateObject: $!"; # create the ADO object $conn->Open('OLE_DB_NWind_Jet'); # connect to the data source # build a simple SQL INSERT $sql = "INSERT into Employees (LastName, FirstName, EmployeeID)"; $sql .= "VALUES ('$lastname', '$firstname', '$empid')"; $conn->Execute($sql); # run it $conn->Close(); |