Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unable to delete

Status
Not open for further replies.

rozzay

Programmer
Jan 3, 2002
142
US
Hello i am trying to ftp a list of files in a single session once done ftp the files successfully i would like to delete the list files. Instead of deleting the files are ftp twice and then error message of "Can't return outside a subroutine" at the line of closedir(EDIOLDTMP)

#!/usr/bin/perl
# The files are moved in batch mode.

my $now=localtime;

open(LOG, ">>/local/logs");
$edioldtmp_dir = "/local/ediold/tmp";

opendir(DIR, $edioldtmp_dir);
opendir(EDIOLDTMP, $edioldtmp_dir) or die "Can't open $dir: $!";

while ( defined ($file = readdir(DIR)) )
{
next if $file =~ /^\.\.?/;
@files = glob("$edioldtmp_dir/*");
$ftpsub_rc=ftpsub();
print "return code= $ftpsub_rc\n";
if ( $ftpsub_rc == 0 )
{
unlink( $edioldtmp_dir . "/" . $file),
print LOG "$now-> File: $file\n:" };
print "$file\n";
print "files deleting\n";
};

sub ftpsub
{
use Net::FTP;
$mode = "ascii";
my $ftp;
$ftp = Net::FTP->new("sample.com", Port => 21, Debug => 1) || return(1);
$ftp->login("userid", "password") || return(1);
$ftp->cwd ("/INBOUND/EDI") || return(1);
foreach $file(@files)
{
$ftp->ascii() if($mode eq "ascii");
$ftp ->put ($file) || return(1);
}
};
return (0);
$ftp->quit() || return(1);
closedir(EDIOLDTMP);
closedir(DIR);
close LOG;
 
Code:
#!/usr/bin/perl
# The files are moved in batch mode.

my $now=localtime;
  
open(LOG, ">>/local/logs");
$edioldtmp_dir = "/local/ediold/tmp";

opendir(DIR, $edioldtmp_dir);
opendir(EDIOLDTMP, $edioldtmp_dir) or die "Can't open $dir: $!";

while ( defined ($file = readdir(DIR)) ) {
  next if $file =~ /^\.\.?/;
  @files = glob("$edioldtmp_dir/*");     
  $ftpsub_rc=ftpsub();
  print "return code= $ftpsub_rc\n";
  if ( $ftpsub_rc == 0 ) {
      unlink( $edioldtmp_dir . "/" . $file),
      print LOG "$now-> File: $file\n:" };
      print "$file\n";
      print "files deleting\n";
  }
}
closedir(EDIOLDTMP);
closedir(DIR);
close LOG;
sub ftpsub {
    use Net::FTP;
    $mode = "ascii";
    my $ftp;
    $ftp = Net::FTP->new("sample.com", Port => 21, Debug => 1) || return(1);
    $ftp->login("userid", "password") || return(1);
    $ftp->cwd ("/INBOUND/EDI") || return(1);
    foreach $file(@files)    
    {
        $ftp->ascii() if($mode eq "ascii");
        $ftp ->put ($file) || return(1);
    }
    $ftp->quit() || return(1); #this needs to be ahead of the return, because when the routine returns, no statements
    return (0);                #will be executed after the fired return statement
}


Your return wasn't actually in the subroutine hence the error

HTH

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top