2017-04-03 14 views
1

マイコード:私は明示的に宣言していてもサブルーチン宣言の問題

Undefined subroutine &main::sendmail called at ./test1.pl line 86.

#!/usr/bin/perl 

use strict; 
use warnings; 

use Web::Scraper; 
use Data::Dumper; 
use Config::Simple; 
use Email::Simple(); 
use Email::Sender::Transport::SMTP; 
use URI; 
use JSON; 
use Digest::MD5 qw(md5_hex); 
use POSIX qw/strftime/; 

my $TimeStamp = strftime('%Y-%m-%d',localtime); 
my $purlToScrape = 'http://www.natboard.edu.in/dnbfinal.php'; 
my $webdata  = scraper {  
    process 'td.noticeboard', "data[]" => 'TEXT';  
}; 
my $dnbnotices = $webdata->scrape(URI->new($purlToScrape)); 
my (@data)  =(); 
my $configfile = '/root/dnbfscrape.conf'; 
my $olddigest; 
my $EmailSub  = ''; 
my $EmailBody = ''; 

sub WriteConfigurationFile; 
sub LoadConfigurationFile; 
sub SendEmail; 

LoadConfigurationFile; 

my $board=$dnbnotices->{data}; 
my @noticeboard = @$board; 

my $count = 0; 

printf ("%3s %-18s %-30s %-30s\n", "No:", "Session", "Title", "Last date"); 

for (my $index = 0; $index <= $#noticeboard; $index += 5) { 
    printf ("%3d %-18s %-30s %-30s\n", ++$count, $noticeboard[$index], $noticeboard[$index+1], $noticeboard[$index+2]); 
} 

my $json_str = encode_json(\@noticeboard); # This will work now 
my $digest = md5_hex($json_str); 

if ($digest eq $olddigest) { 

    print "The page has not changed.\n"; 

    my $EmailSub = 'No change in DNB Final Notices'; 
    my $EmailBody = 'The DNB Notice page for applications has not changed as of '.$TimeStamp."\n"; 

    SendEmail; 
} 

# print "$digest"; 

WriteConfigurationFile($digest); 


sub WriteConfigurationFile { 
    my $digest = shift; 

    # print "\n\n". $digest."\n"; 

    my $cfg = new Config::Simple(syntax => 'ini'); 

    $cfg->param("dnb.digest", $digest); 
    # $cfg->param("old.digest", md5_hex($new_title)); 

    $cfg->write($configfile); 
} 

sub LoadConfigurationFile { 

    my $cfg = new Config::Simple(syntax => 'ini'); 

    $cfg->read($configfile); 
    $olddigest = $cfg->param("dnb.digest"); 
} 

sub SendEmail { 

    my $smtpserver = 'smtp.mandrillapp.com'; 
    my $smtpport  = 587; 
    my $smtpuser  = '[email protected]'; 
    my $smtppassword = 'ed61DZIbxGIKRANRnsWyug'; 

    my $transport = Email::Sender::Transport::SMTP->new({ 
     host   => $smtpserver, 
     port   => $smtpport, 
     sasl_username => $smtpuser, 
     sasl_password => $smtppassword, 
    }); 

    my $email = Email::Simple->create(
     header => [ 
      To  => '[email protected]', 
      From => '[email protected]', 
      Subject => $EmailSub, 
     ], 
     body => $EmailBody."\n", 
    ); 

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

私はエラーを取得します。どうして?

+0

どのsendmail機能を使用しますか? – Jens

+0

実際には、私のコード内で、私は正しく私のサブを呼び出すよ。私のサブSendmail内のsendmailはEmail :: Simpleクラスの一部です。 – Droidzone

+1

パッケージ名 'Email :: Simple :: sendmail' – Jens

答えて

1

SendEmailサブルーチンでは、私はsendmailを宣言したことを忘れずに使用していました。

これは、明示的にそれを宣言することによって解決される:sendmailに呼び出しを使用する前に、

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