Perl - 使用未初始化的变量$ ftp

时间:2018-04-28 01:57:49

标签: perl ftp initialization

运行以下perl脚本时出错。错误如下:

  

在AfterGauss.pl第54行使用未初始化的值。

第54行是$ftp->put($fpath1) || die $ftp->message;。有没有人认为我的代码有问题。我试过删除die语句,第一个ftp就失败了。如果我重新运行脚本它第二次运行正常,但第一次总是抛出这个错误。

use strict;
use warnings;
use Net::FTP;


#proper date.ini file with fully qualified path
my $dateFile = "C:\\CompOps\\PostApps\\ini\\pDate.ini";

my $date;

#Get Date
open FILE, $dateFile || die "could not open $dateFile";
while(<FILE>) {
    $date = $_;
}
close FILE;

#Trim Date
$date =~ s/^\s+|\s+$//g;

#rdc original filename with path
my $fileName1 = "Y:\\PRODUCTS\\XML\\RDCX$date.XML";
#rdc new filename with path
my $newFileName1 = "Y:\\PRODUCTS\\XML\\rdcx$date.xml";
#rdc new filename with path to $archiveFile1
my $archiveFile1 = "Y:\\PRODUCTS\\XML\\Archive\\rdcx$date.xml";
#rom original filename with path
my $fileName2 = "W:\\BBSDATA\\ROMX$date.ZIP";
#rom new filename with path
my $newFileName2 = "W:\\BBSDATA\\romx$date.zip";

#rdc file with path to post from
my $fpath1 = $newFileName1;
#rom file with path to post from
my $fpath2 = $newFileName2;

#rename RDC file to lowercase
rename($fileName1, $newFileName1) || die "Cannot rename $fileName1: $!\n";

#rename ROM file to lowercase
rename($fileName2, $newFileName2) || die "Cannot rename $fileName2: $!\n";

# FTP credentials
my $host = "HOST";
my $user = "USERNAME";
my $pass = "PASSWORD";
#directory to post to
my $dir = "/content-inbound-to-rdc";

#ftp rdc file
my $ftp = Net::FTP->new($host) || die "Can't open $host: $@\n";
$ftp->login($user, $pass) || die $ftp->message;
$ftp->cwd($dir);
$ftp->put($fpath1) || die $ftp->message;
$ftp->quit;
print "rdc ftp successful! ";
print $ftp->message;

#move rdc file to $archive
rename($newFileName1, $archiveFile1) || die "Cannot move rdc file to archive \n";

#update credentials
$host = "HOST";
$user = "USERNAME";
$pass = "PASSWORD";
#update post to path
$dir = '/cedrom';
#ftp rdc file
$ftp = Net::FTP->new($host) || die "Can't open $host: $@\n";
$ftp->login($user, $pass) || die $ftp->message;
$ftp->cwd($dir);
$ftp->put($fpath2) || die $ftp->message;
$ftp->quit;
print "rom ftp successful! ";
print $ftp->message;

1 个答案:

答案 0 :(得分:3)

可能是由某些网络问题引起的,例如中止连接。然后$ftp->message可能是undef,因为服务器无法在此处发送任何消息。但这只是一个猜测,documentation并不清楚$ftp->message实际来自哪里。

这种在第二次尝试中起作用的现象可能会强化这种猜想。有时会出现网络设备故障并且会被唤醒&#34;经过一些交通,最初失败了。

如果你想重试几次,这里有一个草图(未经测试,但应该证明这个想法):

print("put\n");
for (my $counter = 3; $counter; $counter--) {
  if (!$ftp->put($fpath1)) {
    if (defined($ftp->message)) {
      print("Error. Message: $ftp->message\n");
    }
    else {
      print("Error. Message: undef\n");
    }
    print("retrying...\n");
  }
  else {
    last;
  }
}
if (!$counter) {
  print("gave up.\n");
  # further error handling goes here... e.g. abort the script...
}

您可以删除

    if (defined($ftp->message)) {
      ...
    }
    else {
      ...
    }

if (!$counter) {
  ...
}

print()如果你不想分别处理或输出那个。{/ p>

根据需要降低或提高初始计数器值。