pacoverflow
pacoverflow

Reputation: 3881

Perl - undefined subroutine

I have the following Perl code:

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:

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

set_defaults(SSL_verify_mode => SSL_VERIFY_NONE);

Upvotes: 4

Views: 5785

Answers (1)

David W.
David W.

Reputation: 107030

Take a look at the code Email/Sendmail/Simple.pm. There is no sendmail subroutine in that program. 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.

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.

Oh...

So, the purpose of using Sub::Exporter is to export subroutine names that aren't subroutines in your package.

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.

Thus, Email::Sender::Simple::sendmail isn't a subroutine, but that sendmail can still be exported.

Upvotes: 4

Related Questions