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

Please tell me if this is the most efficient way to make sure my File::Find goes into specific directories (in this case I only want to search 3 directories called 'FirstOne', 'SecondTwo', 'ThirdThree'):
use strict; use File::Find; my $directory = '/perl/bin'; find( \&Searcher, map "$directory/$_", qw(FirstOne SecondTwo ThirdThre +e)); sub Searcher { if(-d $_) { print "$File::Find::name\n"; #Do stuff in each directory here ... } }
Any advise on improving this because it does work but want to make sure I am doing it the most efficient way.
Thanks

Replies are listed 'Best First'.
Re: Doing stuff in specific directories on Unix
by sauoq (Abbot) on Jul 28, 2003 at 20:05 UTC
    Any advise on improving this because it does work but want to make sure I am doing it the most efficient way.

    Worry less about efficiency and more about maintainability. There's no reason to put all of that right in the call to find(), is there? Why not make it more readable and easier to extend later by separating the part where you construct the directory names like this

    use strict; use File::Find; my $base = '/perl/bin'; my @dirs = map "$base/$_", qw( FirstOne SecondTwo ThirdThree); find( \&Searcher, @dirs ); # ...

    -sauoq
    "My two cents aren't worth a dime.";
    
      There's no reason to put all of that right in the call to find(), is there?

      Sure there is. If he's just trying to get it done, right now, and he's not likely to use this program ever again, why bother? If he does use it again and needs to make it more flexible, it's just code. He can generalize then.

      While half of the sins in the programming world come from premature optimization, easily another half come from premature generalization. (Yes, there are more sins in programming than you'd believe.) Back in 1999, I just wanted a working web browser, not a cross-platform framework for writing applications in XML and JavaScript.

        If he's just trying to get it done, right now, and he's not likely to use this program ever again, why bother?

        Well, he already said it worked and that he was trying to improve it.

        Back in 1999, I just wanted a working web browser, not a cross-platform framework for writing applications in XML and JavaScript.

        Back in 1999? Heh. That's all I want today.

        -sauoq
        "My two cents aren't worth a dime.";