• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

return function

with code below, I would like to know, where does the return from "chkfile" return to.
would it take me back to chkfile(), then continue with chkfile2().
it does not appear to be, how can I make that happen.

@paths = ("D:\\123\\archive",
"D:\\abc\\archive",
"D:\\def\\archive"
);
foreach $path (@paths) {
chdir $path;
chkfile();
chkfile2();
}

chkfile {
  if (!somefile) {
    return;
  }
  do something;
}

chkfile2 {
  if (!somefile) {
    return;
  }
  do something;
}
0
matgold
Asked:
matgold
  • 9
  • 6
  • 3
  • +1
1 Solution
 
gripeCommented:
The return will return you to the part of your code directly following the function call to chkfile. So:

foreach $path (@paths) {
chdir $path;
chkfile();
# RIGHT HERE
chkfile2();
}

0
 
gripeCommented:
Although, there are some problems with your code....

- when you define a sub, it's supposed to have the sub keyword.
- You should be using 'warnings' and 'strict' for diagnostic purposes, scoping and other benefits.
- your variables don't have sigils

Here's a minor rewrite:


use strict;
use warnings;

my @paths = ("D:\\123\\archive",
"D:\\abc\\archive",
"D:\\def\\archive"
);
foreach my $path (@paths) {
chdir $path;
chkfile();
chkfile2();
}

sub chkfile {
  if (!$somefile) {
    return;
  }
  do something;
}

sub chkfile2 {
  if (!$somefile) {
    return;
  }
  do something;
}
0
 
matgoldAuthor Commented:
Global symbol "@PATHS" requires explicit package

sub chkfile {
  my @files = glob "*.123";     #  BEGIN not safe after errors--compilation
  if (!@files) {
     return;
  }
  do something;
}

sub chkfile2 {
  if (!$somefile) {
    return;
  }
  do something;
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gripeCommented:
Your variable is '@paths' not '@PATHS'.. Perl is case sensitive.

Note also that under 'strict' you must declare (scope) your variables with the my, our, or similar keyword or make package globals with fully qualified names. If you don't scope your variables, you'll get the same error.
0
 
matgoldAuthor Commented:
ok, I got that fixed.
I still don't think the return is working.

under chkfile(), if no file, it is not returning to where I'm calling it from.
because it never call chkfile2
0
 
ozoCommented:
Are you sure it never call chkfile2?  What is $somefile?
0
 
matgoldAuthor Commented:

sub chkfile2 {
 my @files = glob "*.gz";
  if (!@files) {
     return;
  }
  do something;
}
0
 
matgoldAuthor Commented:
it actually does return to the right place.
I think the problem is that, if no file from chkfile, then chkfile2 is not doing the deletion.

sub chkfile {
  my @files = glob "*.123";    
  if (!@files) {
     return;
  }
  foreach my $file (@files) {
     unlink ($file);
  }
}

sub chkfile2 {
  my @files2 = glob "*.123";    
  if (!@files2) {
     return;
  }
  foreach my $file (@files2) {
     unlink ($file);
  }
}
0
 
ozoCommented:
If
  my @files = glob "*.123";    
  if (!@files) {
     return;
  }
returns
then what should chkfile2 delete?
0
 
matgoldAuthor Commented:
OH, I'm sorry , I was doing a cut & paste.
chkfile2 will delete *.zip, *.gz
0
 
ozoCommented:
How do you delete *.zip, *.gz?
Can you delete then when
  if (!@files) {
     return;
  }
does not return?
0
 
matgoldAuthor Commented:
sub chkfile2 {
  my @files2 = glob "*.zip *.gz";    
  if (!@files2) {
     return;
  }
  foreach my $file (@files2) {
     unlink ($file);
  }
}

I run into trouble, when there is no files to delete on chkfile.
then it goes into chkfile2, it found some files, but some how it didn't delete.
maybe, it have something to do with changing directory, see above code.
0
 
ozoCommented:
my @files2 = (glob "*.zip"),(glob "*.gz");
or
my @files2 = glob "*.{zip,gz}";

0
 
matgoldAuthor Commented:
ok, here is the entire code. I substitute unlink with print, just for testing purposes.
it seems to be display all the files, except when there is no files, it won't print the heading.

#!/PERL/bin/perl -w
my $expire_days = 100;
my $cwd = getcwd();
use Time::localtime;
use File::stat;
use File::Basename;
use Cwd;
use strict;
use warnings;
my($atfiles) = "y";

my @PATHS = ( "D:\\123\\archive",
"D:\\abc\\archive",
"D:\\test\\archive",
"D:\\northgate\\archive"
);
foreach my $PATH (@PATHS) {
 my($atfiles) = "y";
chdir $PATH;
chkfile();
chkfile_2();
}

sub chkfile_2 {

if ($atfiles eq 'n') {
 print " ","\n";
 print "Filename                                  Size        Date: \n";
 print "--------                                  ----        ----- \n";
 $cwd = getcwd();
 print " ","\n";
 print "***** $cwd: *****\n";
 print " ","\n";
}

my @files2 = glob "*.{pgp}";
if (!@files2) {
   return;
}

foreach my $file (@files2) {
my $fst = stat( $file);
my $mytime = time();

if ($mytime - $fst->mtime > $expire_days * 86400) {
   my $date_string = ctime($fst->mtime);
   my $fsize = $fst->size;
   printf "%-40s %-10s %-24s\n",$file, $fsize, $date_string;
}
}
}

sub chkfile {
my @files = glob "*.{2*} *.{aand} *.{gab*}";

if (!@files) {
   my($atfiles) = "n";
   return;
}
print " ","\n";
print "Filename                                  Size        Date: \n";
print "--------                                  ----        ----- \n";
$cwd = getcwd();
print " ","\n";
print "***** $cwd: *****\n";
print " ","\n";
foreach my $file (@files) {
my $fst = stat( $file);
my $mytime = time();

if ($mytime - $fst->mtime > $expire_days * 86400) {
   my $date_string = ctime($fst->mtime);
   my $fsize = $fst->size;
   print "%-40s %-10s %-24s\n",$file, $fsize, $date_string;
}
}
}
0
 
ozoCommented:
#@files = glob "*.{2*} *.{aand} *.{gab*}"; #the spaces here are treated as part of the filename
#I presume you mean
my @files = glob("*.2*"),glob("*.aand"),glob("*.gab*");
or
my @files = glob("{*.2*,*.aand,*.gab*}");
or
my @files = glob("*.{{2,gab}*,aand}")
0
 
matgoldAuthor Commented:
that doesn't seem to be the problem.
0
 
ozoCommented:
If you want to print the heading even when there are no files, then
print the heading before you return

print " ","\n";
print "Filename                                  Size        Date: \n";
print "--------                                  ----        ----- \n";
$cwd = getcwd();
print " ","\n";
print "***** $cwd: *****\n";
print " ","\n";
if (!@files) {
   my($atfiles) = "n";
   return;
}
0
 
manav_mathurCommented:
>my @files2 = (glob "*.zip"),(glob "*.gz");

Ozo, shouldnt this be

my @files2 = ((glob "*.zip"),(glob "*.gz"));
0
 
matgoldAuthor Commented:
is not the exact answer I'm expecting, but OK.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 9
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now