2017-04-03 85 views
1

我的代码:子程序声明问题

#!/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 }); 
} 

我得到的错误:

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

即使我明确声明它。为什么?

+0

你想使用哪个sendmail函数? – Jens

+0

其实,在我的代码中,我正确地调用了我的子集。我的子Sendmail中的sendmail是Email :: Simple类的一部分。 – Droidzone

+1

你必须称它为包名'Email :: Simple :: sendmail' – Jens

回答

1

在SendEmail子程序中,我曾使用sendmail而不记得声明它。

这是通过显式声明它解决了:

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

使用调用sendmail之前,。