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

Gooday, i have written a short program that searches for a given directory. This works fine as long as i use a directory that i designed especially for this purpose(C:\Strawberry). If i change this:

$mp = "C:/Strawberry/";

into this: $mp = "C:/";

my code no longer works. Does someone know why?

print "What folder?\n"; $term = <>; chomp $term; $mp = "C:/Strawberry/"; opendir($dh,$mp) || die "wtf??"; while( $tekst = readdir($dh)){ if(-d "$mp$tekst" && $tekst !~ /^\./){ if($term eq $tekst){ print "found: $mp$tekst\n"; } else{ $hulp ="$mp$tekst"; godeep($hulp); } } } close $dh; sub godeep{ $fld = shift; opendir($dir,$fld); while($tekst2= readdir($dir)){ if(-d "$fld/$tekst2" && $tekst2 !~/^\./) { if($term eq $tekst2){ print "found: $fld/$tekst2\n"; } else{ $hulp2 = "$fld/$tekst2"; godeep($hulp2); } } } }

Replies are listed 'Best First'.
Re: search for folder
by Corion (Patriarch) on Dec 23, 2017 at 17:30 UTC

    You don't tell us where your code goes wrong and what it does, and what you expect instead.

    I'm guessing without running your code.

    You are not using lexical variables where you should use them. In godeep(), $dir and $fld get overwritten once the routine calls itself because $dir and $fld are global variables instead of lexical variables.

    Once you declare $fld and $dir as lexical variables, they won't get overwritten when godeep() calls itself:

    my( $fld ) = shift; opendir( my $dir, $fld ) or die "Couldn't opendir '$fld': $!";

    If you want Perl to help you declare your variables, add this line to the top of your script:

    use strict;

    That makes Perl force you to decide for every variable whether it should be global or lexical.

Re: search for folder
by atcroft (Abbot) on Dec 23, 2017 at 17:34 UTC

    I admit I have not tried running your code, but I will make the following suggestions, which I strongly suspect will lead you to your answer:

    • when performing a file or directory operation, make sure you test the result, and
    • if it fails (especially while still under development) consider looking at the error variable $!.

    I say this because I noticed that in your main routine you perform an opendir() and die() on error with a custom error message, ans in your godeep() routine you perform an opendir() and assume success.

    Hope that helps.

Re: search for folder
by 1nickt (Canon) on Dec 23, 2017 at 18:06 UTC

    Hi, make it easy on yourself and use a tool built for the job:

    perl -MPIR -wE 'chomp(my $wanted = <>); say for PIR->new->dir->name(qr +/$wanted/)->all("C:/")'
    See Path::Iterator::Rule.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: search for folder
by thanos1983 (Parson) on Dec 23, 2017 at 22:23 UTC

    Hello Anonymous Monk,

    Why to write your own program when there are modules that do exactly what you want to do and most likely they do a better job?

    For example: File::Find or File::Find::Rule.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: search for folder
by Anonymous Monk on Dec 24, 2017 at 08:07 UTC

    Thank you all for replying. After i declared the variables it is working. Best regards.