Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Still problems with recursive coding.

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


in reply to Still problems with recursive coding.

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......
  • Comment on Re: Still problems with recursive coding.

Replies are listed 'Best First'.
RE: Re: Still problems with recursive coding.
by jima (Vicar) on Nov 13, 2000 at 20:32 UTC
    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 ;-) ...)
RE: Re: Still problems with recursive coding.
by Fastolfe (Vicar) on Nov 13, 2000 at 20:43 UTC
    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
        For brevity's sake:
        my $found_any = 0; ... sub search_all { unlink, $found_any++ if /.../; } # (repeat for search_none) print "No files found!\n" unless $found_any;
        Make sense? You basically set a flag (a variable) if you find anything to unlink, and when your script is completed, check to see if this variable has a value, and if it doesn't, that means nothing matched, and you can alert the user to this fact. You can go the other way as well:
        if ($found_any) { print "$found_any files deleted.\n"; } else { print "No files were found to delete.\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found