你熟悉面向对象的 Perl 是如何工作的吗?
为了使用面向对象的 Perl 模块,您必须首先创建该类类型的对象。通常,这是通过以下new
方法完成的:
my $smtp = Net::SMTP->new($mailhost);
现在,是class$smtp
的一个对象Net::SMTP
。基本上,它是对 glob 的引用,您可以在其中存储数据结构(您发送给谁、您的消息等)。然后 Perl 可以在方法调用期间使用这些信息(它们只是作为包 Net::SMTP 的一部分的子例程)。
这是我编写的程序中的一个示例:
use Net::SMTP;
my $smtp = Net::SMTP->new(
Host => $watch->Smtp_Host,
Debug => $debug_level,
);
if ( not defined $smtp ) {
croak qq(Unable to connect to mailhost "@{[$watch->Smtp_Host]}");
}
if ($smtp_user) {
$smtp->auth( $watch->Smtp_User, $watch->Smtp_Password )
or croak
qq(Unable to connect to mailhost "@{[$watch->Smtp_Host]}")
. qq( as user "@{[$watch->Smtp_User]}");
}
if ( not $smtp->mail( $watch->Sender ) ) {
carp qq(Cannot send as user "@{[$watch->Sender]}")
. qq( on mailhost "@{[$watch->Smtp_Host]}");
next;
}
if ( not $smtp->to($email) ) {
$smtp->reset;
next; #Can't send email to this address. Skip it
}
#
# Prepare Message
#
# In Net::SMTP, the Subject and the To fields are actually part
# of the message with a separate blank line separating the
# actual message from the header.
#
my $message = $watch->Munge_Message( $watcher, $email );
my $subject =
$watch->Munge_Message( $watcher, $email, $watch->Subject );
$message = "To: $email\n" . "Subject: $subject\n\n" . $message;
$smtp->data;
$smtp->datasend("$message");
$smtp->dataend;
$smtp->quit;