Saturday 15 September 2012

Die statement is not working in foreach loop in perl -



Die statement is not working in foreach loop in perl -

my script downloads files ftp , moves files archive directory in ftp. have pattern search files in ftp, , have set in foreach loop files local directory.

my $ftpuser = 'xxxx'; $ftppw = 'xxxxxx'; $ftphost = "xxxxxxxxxxx"; $remotefile = 'cap*.csv'; $archivefile = "/usr/archive/cap_${file_date_time}.csv"; $ftp = net::ftp->new($ftphost); $ftp->login( $ftpuser, $ftppw ) or die print l1 "could not login ftp :" . $ftp->message . "\n"; print l1 "login ftp successfull\n"; $ftp->cwd("/") or die print l1 "ftp_cd failed: " . $ftp->message . "\n"; foreach $file_to_fetch ( $ftp->ls($remotefile) ) { $ftp->get( $file_to_fetch, $localfile ) or die print l1 "could not file ftp :" . $ftp->message . "\n"; $remotefile = $file_to_fetch; print "\$file_to_fetch ::: $file_to_fetch\n"; print l1 "file - ${file_to_fetch} downloaded ftp\n"; $ftp->rename( $file_to_fetch, $archivefile ) or die print l1 "could not move file archive directory :" . $ftp->message . "\n"; print l1 "file - ${file_to_fetch} moved archive directory cap_${file_date_time}.csv\n"; } $ftp->quit; print l1 "ftp process completed\n"; if ( -s $localfile ) { open f1, "$localfile" or die( print l1 "$localfile cannot opened reading \n" ); } else { die( print l1 "$localfile not exist \n" ); }

while executing above code, if file searching not there in ftp not printing die statement "could not file ftp " log, instead comes out of ftp , proceeds next set of code print l1 "ftp process completed\n".

please help me on these, why die statement not working in foreach, if not able files ftp.

replace,

$ftp->get($file_to_fetch,$localfile) or die print l1 "could not file ftp :" . $ftp->message ."\n";

with

$ftp->get($file_to_fetch,$localfile) or die "could not file ftp :". $ftp->message ."\n";

as die in first case takes print() homecoming value argument instead of error message.

alternatively create own function,

sub mydie { ($msg) = @_; print l1 $msg; die $msg; }

or if function not option,

$ftp->get($file_to_fetch,$localfile) or { print(l1 $_), die($_) "could not file ftp :". $ftp->message ."\n"; };

perl

No comments:

Post a Comment