perl ftp无法正常工作

时间:2011-10-02 15:02:44

标签: perl

脚本工作正常,但ftp代码上传xls但上传时为0字节,但如果ftp代码出现在以下代码段之前,FTP Works Fine,

代码中的错误是什么,

my $workbook = Spreadsheet::WriteExcel->new('TestRPT.xls'); my $worksheet = $workbook->add_worksheet('TestRPT Report');

#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Spreadsheet::WriteExcel;
use POSIX qw(strftime);


my $CurTimeStamp=time;
my $LastSunTimestamp=($CurTimeStamp - 168*60*60);
my $row;
my $PinNumber;
my $PinAmount;
my $get_date;
my $get_time;
my $get_time_stamp;
my $DoFTPFlg = "yes";


# Create a new workbook and add a worksheet.
my $workbook  = Spreadsheet::WriteExcel->new('TestRPT.xls');
my $worksheet = $workbook->add_worksheet('TestRPT Report');


# Write some text. in write function First Argument for ROW, Second Argument for COLUMN, Third Argument for Title/Text to display
$worksheet->write(0, 0,  'val1');
$worksheet->write(0, 1,  'val2');
$worksheet->write(0, 2,  'val3');
$worksheet->write(0, 3,  'val4');
my $cnt = 1;
    $get_time_stamp = time;
    $get_date = strftime("%m/%d/%y",localtime($get_time_stamp));
    $get_time = strftime("%H:%M",localtime($get_time_stamp));
    # Write some numbers.
    $worksheet->write($cnt, 0,  "val1");          
    $worksheet->write($cnt, 1,  "val2");    
    $worksheet->write($cnt, 2,  "val3"); 
    $worksheet->write($cnt, 3,  "val4"); 




if ($DoFTPFlg eq "yes") {
    print "DO FTP";
    use Net::FTP;
    my $ftp;
    $ftp = Net::FTP->new("mysite.in", Debug => 0);
    $ftp->login("user",'pass');
    $ftp->cwd("/www/");
    $ftp->put("TestRPT.xls");
    $ftp->quit; 

}

2 个答案:

答案 0 :(得分:3)

在尝试对文件执行任何操作之前,您应该close $workbook个对象。

来自documentation

  

如果必须在对文件执行某些外部操作之前关闭文件,例如复制文件,读取文件大小或将其附加到电子邮件,则需要显式关闭()。

答案 1 :(得分:1)

尝试对代码稍作修改。而不是

$ftp->put("TestRPT.xls");

www目录中创建另一个文件并尝试ftp该文件。如果该文件名为test.txt,请将您的行更改为:

$ftp->put("TestRPT.xls");

因此,代码中唯一的变化就是FTP'd文件的名称。如果您的FTP有效,问题不在于FTP,而在于Spreadsheet::WriteExcel模块。正如Mat已经说明的那样,您需要在对象上做一个明确的close

如果您的FTP不起作用,则可能是FTP呼叫的问题(虽然它对我来说很好)。