matgold
asked on
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;
}
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;
}
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;
}
- 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;
}
ASKER
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;
}
sub chkfile {
my @files = glob "*.123"; # BEGIN not safe after errors--compilation
if (!@files) {
return;
}
do something;
}
sub chkfile2 {
if (!$somefile) {
return;
}
do something;
}
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.
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.
ASKER
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
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
Are you sure it never call chkfile2? What is $somefile?
ASKER
sub chkfile2 {
my @files = glob "*.gz";
if (!@files) {
return;
}
do something;
}
ASKER
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);
}
}
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);
}
}
If
my @files = glob "*.123";
if (!@files) {
return;
}
returns
then what should chkfile2 delete?
my @files = glob "*.123";
if (!@files) {
return;
}
returns
then what should chkfile2 delete?
ASKER
OH, I'm sorry , I was doing a cut & paste.
chkfile2 will delete *.zip, *.gz
chkfile2 will delete *.zip, *.gz
How do you delete *.zip, *.gz?
Can you delete then when
if (!@files) {
return;
}
does not return?
Can you delete then when
if (!@files) {
return;
}
does not return?
ASKER
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.
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.
my @files2 = (glob "*.zip"),(glob "*.gz");
or
my @files2 = glob "*.{zip,gz}";
or
my @files2 = glob "*.{zip,gz}";
ASKER
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;
}
}
}
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;
}
}
}
#@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}")
#I presume you mean
my @files = glob("*.2*"),glob("*.aand"
or
my @files = glob("{*.2*,*.aand,*.gab*}
or
my @files = glob("*.{{2,gab}*,aand}")
ASKER
that doesn't seem to be the problem.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>my @files2 = (glob "*.zip"),(glob "*.gz");
Ozo, shouldnt this be
my @files2 = ((glob "*.zip"),(glob "*.gz"));
Ozo, shouldnt this be
my @files2 = ((glob "*.zip"),(glob "*.gz"));
ASKER
is not the exact answer I'm expecting, but OK.
foreach $path (@paths) {
chdir $path;
chkfile();
# RIGHT HERE
chkfile2();
}