我有一个脚本Attachments.pm
,其中包含
package app::Attachments;
use MIME::Lite;
BEGIN {
use Exporter();
@ISA = qw(Exporter);
@EXPORT = qw(&SendEMmsgAttachments);
}
sub SendEMmsgAttachments {
$EM_SERVER = "1234.com";
$EM_FROM = "yyy@1234.com"; #hardcoded
$EM_TIMEOUT = 120;
my $mailMessage;
my $mailToEmailAddress;
my $mailSubject;
my $mailBody;
my $mailAttachmentFileName;
my $mailAttachmentFullPath;
$mailMessage = MIME::Lite->new(
From => $EM_FROM,
To => $mailToEmailAddress,
Subject => $mailSubject,
Type => 'multipart/mixed'
) or die "Error creating multipart container: $!\n";
### Add the text message part
$mailMessage->attach(
Type => 'text/csv',
Data => $mailBody
) or die "Error adding the text message part: $!\n";
### Add the text file
$mailMessage->attach(
Encoding => 'base64',
Type => "text",
我想在SendEMmsgAttachments
文件中使用testscript.pl
,以便我可以通过电子邮件发送Excel附件。
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
这是一个非常基本的hello世界,使用perl模块和脚本,演示如何使用Exporter库。有关详细信息,请参阅perldoc Exporter。
<强> Foo.pm 强>
package Foo;
use strict;
use warnings FATAL => 'all';
use Exporter 'import';
our $VERSION = '0.01';
our @EXPORT_OK = qw( bar );
sub bar {
return "Hello World";
}
1; # Last statement of a .pm file must evaluate to 'true'
<强> try_foo.pl 强>
#!/usr/bin/env perl
use warnings;
use strict;
use Foo qw( bar );
my $msg = bar();
print $msg . "\n";
在行动中:
perl try_foo.pl
Hello World
答案 1 :(得分:1)
在你的程序中你有这一行:
use Exporter();
这指示Perl加载Exporter
模块,但不从中导入任何内容。括号表示您要提供自己的导入列表,而不是接受默认列表。由于导入列表中没有任何内容,因此不会导入任何内容。
您可以通过将Exporter
添加到@ISA
继承来解决此问题。那里有大量的示例代码可以做到这一点。但是,您只需要import
例程,而不是导出工具的更具体版本。您只需要import
例程:
use Exporter qw(import);
之后,您需要在@EXPORT
中默认指定要导出的内容。对于子程序,请不要使用&
。这不是什么大不了的事,但看起来很常见:
our @EXPORT = qw( SendEmmsgAttachments );
如果您希望程序专门要求导入子程序,可以将其放在@EXPORT_OK
中。默认情况下不会导出这些条目 - 如果您要求它们,则允许导出它们:
our @EXPORT_OK = qw( SendEmmsgAttachments );
our
用于将变量声明为包变量。你的节目不会因此而烦恼,因为你不是use strict
(这不是世界末日,而是一个好习惯)。
我带你的BEGIN
块并将其替换为:
use Exporter qw(import);
our @EXPORT_OK = qw( SendEmmsgAttachments );
遇到这样的问题时,请创建一个显示问题的最小示例,以便消除可能存在问题的任何其他问题。