The Perl Code
March 20, 2000
The Perl code for the object appears below
[ with in-line comments ]
package WebMail;
use Mail::Sender;
sub send {
my($from, $replyto, $to, $cc,
$bcc, $smtp, $subject,
$message, $file) = @_;
my $sender;
# retvalue reflects the value that is going to
# be returned from this sub and its value is
# an indication of whether the mailing operation
# has succeeded or not -1 is true, 0 is false.
my $retValue=-1;
# attempt to construct the Mail::Sender
# object that is going to perform the mailing
# operation
ref ($sender = Mail::Sender->new({
from => $from,
replyto => $replyto,
to => $to,
cc => $cc,
bcc => $bcc,
smtp => $smtp,
subject => $subject,
})) or $retValue=0;
# if the construction of the Mail::Sender
# object has succeeded, then attempt to send the
# message.
if($retValue!=0) {
# the method that is used to send the message
# out depends on whether a file needs to be
# attached to the mail or not.
if(!defined $file) {
ref($sender->MailMsg({
msg=>$message
})) or ($retValue=0);
} else {
ref($sender->MailFile({
msg=>$message,
file=>[(split/\,/,$file)]
})) or ($retValue=0);
}
# if the transmission of the message
# succeeded, close the mailer object
if($retValue!=0) {
$sender->Close();
}
}
# return the result of attempting to send
# the specified message
return($retValue);
}
sub getError {
return ($Mail::Sender::Error);
}
1;
This is all relatively straightforward. We have written a
mail object that takes a number of parameters, and sends a
message based upon them. Really the only point worth noting
is that the return value of the "send" subroutine within
the Perl is set to either 0 or -1. This is because these
values correspond to boolean false and true [ respectively ]
within VBScript, and we are going to test our completed COM
object by calling it from an ASP page.
All the work of actually performing the mailing is
performed by a class called Mail::Sender. Mail::Sender
is a standard CPAN module, and it is your responsibility
to ensure that the module is installed on your system prior
to attempting this example.
So now we must consider how to wrap the Perl objects up as
COM components. In order to do this, we have to use the PDK to
generate the IDL description of our object, as well as package
the object into a Dynamic Link Library [DLL] that is going to
be registered with the system.
Think of a DLL as a repository
of code that can be dynamically referenced at runtime - thus
importing the objects that it contains into memory for use.
Conceptually, It is not a million miles removed from the Perl
"require" command. The DLL will contain your COM object.
The PDK needs help, however, in order to generate the
IDL description of your object. It needs you to write
a template [ which is pure Perl code ], that describes
the functionality of your object in terms of COM. The
template itself is boilerplate - the PDK will generate
it automatically. All you need to do is edit it to reflect
the functionality of your object. The PDK will interpret
it and generate the IDL description of your class, and
package it all up into a COM object.
Next month we'll see how a simple component uses the Active
State package to actually register a COM component with the Win32
registry so that we can use it from any COM-aware program.
How do I package my PERL code as a COM component?
Introduction to Perl on Windows - Table of Contents
Generate the Template
|