Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Still problems with recursive coding.

by curtisb (Monk)
on Nov 13, 2000 at 20:21 UTC ( [id://41332]=perlquestion: print w/replies, xml ) Need Help??

curtisb has asked for the wisdom of the Perl Monks concerning the following question:

Hopefully, this will be last time I will have to write a recursive code without using File::Find module. This code is being written for when I leave my current job, the person who comes after me can maintain it easily.
Now, the problem, the code executes but returns nothing. It is suppost to return "No files deleted if nothing is done", but I don't even get that. I thought that the problem was in the $workdir string, but it doesn't appear to be that. Hopefully, someone, can shed some light and wisdom my way.
#!/usr/bin/perl -ws use strict; use Cwd; my $mode; &op; &scandir('c:\\mydocu~1'); sub op { if (defined @ARGV && $ARGV[0] =~ m/-all/) { $mode="all"; }else{ $mode="none"; } } sub scandir { my ($workdir) = shift || die "No working directory set: $!\n"; my ($startdir) = &cwd; chdir($workdir) || die "Unable to change to $workdir: $!\n"; opendir(DIR, ".") || die "Unable to open $workdir: $!\n"; my @files = readdir(DIR) || die "Unable to read $workdir: $!\n"; closedir(DIR) || die "Unable to close $workdir: $!\n"; foreach my $file (@files) { next if ($file eq "."); next if ($file eq ".."); if ($mode eq "all") { if (-d $file) { &scandir($file); next; } if (-f $file) { if($file =~ m/\.\d+\w+\-\d+\wm$/i || $file =~ m/\.log$/i) { unlink($file) || warn "Unable to delete: $file: $!\n"; }else{ print "No files to delete!, All script\n"; } } } elsif ($mode eq "none") { if(-d $file) { &scandir($file); next; } if (-f $file) { if($file =~ m/\.\d+\w+\-\d+\wm$/i) { unlink($file) || warn "Unable to delete: $workdir\/$fi +le: $!\n"; }else{ print "No files to delete!, None script\n"; } } } chdir($startdir) || die "Unable to change to $startdir: $!\n"; } } &scandir(".");
Does anyone have any suggestions to what the problem might be?
curtisb -- "Becareful, what you wish for, you just might get it!"

Replies are listed 'Best First'.
Re: Still problems with recursive coding.
by merlyn (Sage) on Nov 13, 2000 at 20:23 UTC
    Please use File::Find, and your programming will be able to focus on the core issues. It's silly to reinvent that code. It's even sillier to try to debug your own code while you are attempting to do that.

    -- Randal L. Schwartz, Perl hacker

Re: Still problems with recursive coding.
by curtisb (Monk) on Nov 13, 2000 at 20:27 UTC
    Thanks for the advice. I know that File::Find is easier, but I'm looking at this from a maintain view, for someone with little or no experiance with perl.
    Any clues on the above code
    curtisb......
      Well, with respect to maintainability, I would think that File::Find would be much easier to maintain, once you understand how it works.

      As for your code, the problem is the line

      my @files = readdir(DIR) || die "Unable to read $workdir: $!\n";
      The || operator has a higher precedence than the assignment, which means the readdir() gets evaluated in a scalar context; i.e., it returns only one directory entry. Try the or operator, for the effect you're looking for:
      my @files = readdir(DIR) or die "Unable to read $workdir: $!\n";
      (Now, if you had been using File::Find, you wouldn't have had to figure this out ;-) ...)
      Someone with little or no experience with Perl is going to find it impossible to maintain your code as you have it above. Even we are having a hard time with it.
      use File::Find; use strict; my $start_from = 'c:\mydocu~1'; if (shift(@ARGV) =~ /all/) { find(\&search_all, $start_from); } else { find(\&search_none, $start_from); } sub search_all { unlink if /\.\d+\w+\-\d+\wm$|\.log$/i; } sub search_none { unlink if /\.\d+\w+\-\d+\wm$/i; }
      Note that I'm not having it print "No files to delete". If you want that feature you'll have to set a flag or something when you delete something, and check for that flag when your script exits. As you're doing it in your code, you'll get a "No files to delete" message for every file that does not match your regexp criteria, which is probably not what you want.
        That is a much easier way of doing it. One question, how do you set the flags you are talking about?
        I also took a look at the else statements I wrote and you are correct. Why do I want to print out the statement once for everyfile that does not match? So I corrected protion of the code. Once again you are the man, thanks.
        curtisb

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://41332]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found