子程序声明问题

时间:2017-04-03 08:41:39

标签: perl subroutine

我的代码:

#!/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     = 'servermailer@eyrie.in';
    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      => 'joel@eyrie.in',
            From    => 'servermailer@eyrie.in',
            Subject => $EmailSub,
        ],
        body   => $EmailBody."\n",
    );

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

我收到错误:

  

未定义的子程序&amp; main :: sendmail在./test1.pl第86行调用。

即使我明确宣布它。为什么呢?

1 个答案:

答案 0 :(得分:1)

在SendEmail子例程中,我使用了sendmail而没有记住声明它。

这可以通过以下方式明确声明来解决:

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

在使用sendmail调用之前。