新手请教:写了一个程序,有点错误,请高手指点一下。
新手请教:写了一个程序,有点错误,请高手指点一下。
刚学perl,写了一个FTP拷贝文件的程序,但运行有点问题,请帮忙看一下。
好像那个$file定义的有问题吧,好像后面的}也以没有对应正确,我查了半天没查出来,郁闷。。。
error信息和脚本如下:
error info:
# perl netftp.pl
Found = in conditional, should be == at netftp.pl line 58.
syntax error at netftp.pl line 70, near "or"
syntax error at netftp.pl line 72, near "foreach "
Global symbol "$file" requires explicit package name at netftp.pl line 72.
Global symbol "$file" requires explicit package name at netftp.pl line 73.
Global symbol "$file" requires explicit package name at netftp.pl line 74.
Global symbol "$file" requires explicit package name at netftp.pl line 78.
Global symbol "$file" requires explicit package name at netftp.pl line 80.
Global symbol "$ftp" requires explicit package name at netftp.pl line 88.
syntax error at netftp.pl line 95, near "}"
Execution of netftp.pl aborted due to compilation errors.
Script:
#!/usr/bin/perl -w
use strict;
use Net::FTP;
&main;
sub main() {
my $Datafile="config.txt";
open CONFIGDATA,"<$Datafile" or die "Open file error";
my @data=qw//;
my %host1;
my %host2;
my %host3;
while (<CONFIGDATA>) {
chomp;
unless (/^#.*/){
@data=split/\s+/;
foreach($data[0]) {
for($_){
/host1/ && do {
$host1{host}=$data[1];
$host1{user}=$data[2];
$host1{passwd}=$data[3];
$host1{origdir}=$data[4];
#$host1{para}=$data[4]; for more para
last
};
/host2/ && do {
$host2{host}=$data[1];
$host2{user}=$data[2];
$host2{passwd}=$data[3];
$host2{origdir}=$data[4];
#$host2{para}=$data[4]; for more para
last
};
/host3/ && do {
$host3{host}=$data[1];
$host3{user}=$data[2];
$host3{passwd}=$data[3];
$host3{origdir}=$data[4];
#$host3{para}=$data[4]; for more para
last
}
}
}
}
}
foreach (qw/host user passwd/){
print "host1:$_=$host1{$_}\n";
my $ftp = Net::FTP->new($host1{host}, Debug => 0)
or die "Cannot connect to $host1{host}! ";
$ftp->login($host1{user},$host1{passwd})
or die "Cannot login ", $ftp->message;
$ftp->cwd($host1{origdir})
or die "Cannot change working directory ", $ftp->message;
my @List=$ftp->ls();
or die "Cannot get List of files", $ftp->message;
my $num=0;
foreach $file (@List) {
print"$num:$file!";
my($name,$expand)=split(/\./,$file);
print "$name:$expand\n";
if ($expand=~/^csv$/)
{
$ftp->get($file) or die "get failed ", $ftp->message;
my($newexpand)=".csv_";
my($oldname)=$file;
my($newname)=$name.$newexpand;
$ftp->rename($oldname,$newname) or die "rename faild", $ftp->message;
}
$num++;
}
$ftp->quit;
# print "host2:$_=$host2{$_}\n";
# print "host3:$_=$host3{$_}\n";
}
}