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

This is really frustrating....what my program is supposed to do, is take a directory path as input from a user, and get the contents of the said directory. the program runs on a windows system, btw.
$dir = <>; # get directory from STDIN $dir =~ s/\//\\/g; # clean up the path a bit opendir(DIR,"$dir"); # open directory $dir @files = readdir(DIR); # read directory contents into @files closedir(DIR);
the problem is, readdir() doesn't return any results. the directories I enter are full of files, when i run the debugger, $dir is indeed a perfectly fine directory path, and it works fine when i put a static path in opendir() instead of $dir (such as "c:\\windows\\mycrap"). i can't understand why it isn't working. And i'm not using strict or anything.

Replies are listed 'Best First'.
Re: Can't seem to see the problem here...
by lemming (Priest) on Sep 13, 2001 at 04:07 UTC

    Check the return status of your opendir with opendir(DIR."$dir") or die "Could not open $dir: $!" My bet is on your needing a chomp to get rid of a newline

Re: Can't seem to see the problem here...
by princepawn (Parson) on Sep 13, 2001 at 03:42 UTC
    Hi, a few hints, though I dont have a full solution.

    run the file test operators on what you think is a valid typed-in path:

    -d $dir -f $dir -e $dir # or just call stat()

    you may have to chomp() the input to get rid of the CRLF at the end.

Re: Can't seem to see the problem here...
by clintp (Curate) on Sep 13, 2001 at 04:25 UTC
    I'm betting on the newline as a previous poster said.

    Two nits. First, you don't have to convert c:/windows/mycrap to c:\\windows\\mycrap. Forward slashes are just fine. In fact, they're actually *better* because you don't ever have to worry about counting backslashes, miscounting, getting an odd number of them, and then having all kinds of weird escaping issues. And they're Unix compatibile.

    Second nit, the quotes in the opendir aren't necessary. opendir(DIR, $dir) || warn "opendir $dir: $!" is just peachy.

Re: Can't seem to see the problem here...
by grinder (Bishop) on Sep 13, 2001 at 15:42 UTC

    Using s/// to substitute single characters is woefully inefficient. It is much better to use tr, the transliteration operator:

      $dir =~ tr{/}{\\};

    But, as has already been pointed out, even bothering to do so in this context is redundant. Another redundant operation you are performing is interpolating $dir into a string that contains nothing else "$dir". Just say $dir.

    ...And i'm not using strict or anything.

    Well, you should be.

    --
    g r i n d e r
Re: Can't seem to see the problem here...
by ducky (Scribe) on Sep 13, 2001 at 11:46 UTC

    I believe what you're running into is the fact that when reading from the user via <> is that you're getting new lines, as well. So when you give it "c:\temp" and hit enter, $dir is filled with "c:\\temp\n".

    I suggest the following modification to your code:

    $dir = <>; # get directory from STDIN chomp $dir ; # get rid of trailing new line! $dir =~ s/\//\\/g; # clean up the path a bit opendir(DIR,"$dir") or die "Can't open '$dir' $!" ; # open directory $ +dir @files = readdir(DIR); # read directory contents into @files closedir(DIR);
    The addition of die is to make sure you get a message about why it's not working, pinpointing what's wrong. You could use warn instead and put that in a loop, waiting for a valid dir or some such. Just an idea =)

    -Ducky