简体   繁体   English

Perl - 未定义的子例程

[英]Perl - undefined subroutine

I have the following Perl code: 我有以下Perl代码:

use Email::Sender::Simple;
use IO::Socket::SSL;

IO::Socket::SSL::set_defaults(SSL_verify_mode => SSL_VERIFY_NONE);

Email::Sender::Simple::sendmail($email, { transport => $transport });

When I run it I get this error: 当我运行它时,我收到此错误:

Undefined subroutine &Email::Sender::Simple::sendmail called at script.pl line 73.

If I change the code to have the following, then it works: 如果我将代码更改为具有以下内容,那么它的工作原理如下:

use Email::Sender::Simple qw(sendmail);

sendmail($email, { transport => $transport });

Can someone explain why I had to change the code for sendmail, while I did NOT have to change the code for set_defaults to look like: 有人可以解释为什么我必须更改sendmail的代码,而我没有必要将set_defaults的代码更改为:

use IO::Socket::SSL qw(set_defaults);

set_defaults(SSL_verify_mode => SSL_VERIFY_NONE);

Take a look at the code Email/Sendmail/Simple.pm . 看看代码Email/Sendmail/Simple.pm There is no sendmail subroutine in that program. 该程序中没有sendmail子例程。 Instead, if you look at the header, you'll see: 相反,如果你看一下标题,你会看到:

use Sub::Exporter -setup => {
  exports => {
    sendmail        => Sub::Exporter::Util::curry_class('send'),
    try_to_sendmail => Sub::Exporter::Util::curry_class('try_to_send'),
  },
};

I'm not familiar with Sub::Exporter , but I did notice this description. 我不熟悉Sub :: Exporter ,但我确实注意到了这个描述。

The biggest benefit of Sub::Exporter over existing exporters (including the ubiquitous Exporter.pm) is its ability to build new coderefs for export, rather than to simply export code identical to that found in the exporting package. Sub :: Exporter相对于现有出口商(包括无处不在的Exporter.pm)的最大好处是能够为导出构建新的coderef,而不是简单地导出与导出包中的代码相同的代码。

Oh... 哦...

So, the purpose of using Sub::Exporter is to export subroutine names that aren't subroutines in your package. 因此,使用Sub::Exporter的目的是导出不是包中子例程的子例程名称。

If you're interested, you can read the tutorial of Sub::Exporter , but it appears it has the ability to export subroutines under different names. 如果您有兴趣,可以阅读Sub :: Exporter的教程,但它似乎能够以不同的名称导出子程序。

Thus, Email::Sender::Simple::sendmail isn't a subroutine, but that sendmail can still be exported. 因此, Email::Sender::Simple::sendmail不是子例程,但仍然可以导出sendmail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM