Skip to content

this is nonsense

Ok, I have two files: dirread.pl and simpledirtest.pl. contents are as follows:

### dirread.pl
# this is a test program
# it tries to read the names of files in a directory to a file

print “Please enter the name of the directory you want to list.\n”;
print “Be careful of capitalization.\n”;
print “: “;

$dirname = <STDIN>;

# ok, i’ve got the name of the directory to read; now how about opening the output stream?

open(OUT, ‘> filelist.txt’) or die “can’t open file for output! $!”;

# what are the chances that eliminating the error handling code eliminates the error?
# nope, that just eliminated the error message. putting back the code.
opendir(DIR, $dirname) or die “can’t opendir $dirname: $!”;
while (defined($file = readdir(DIR))) {
# do something with “$dirname/$file”
print OUT $file . “\n”;
}
closedir(DIR);

### simpledirtest.pl
opendir(DIR, “.”); # . is the current directory

while ( $filename = readdir(DIR) ) {
print $filename , “\n”;
}

closedir DIR;

Then, I have output as follows:

C:\My Documents\programs\perl\musicfix\test>perl -w simpledirtest.pl
.
..
a.txt
renamer.pl
test.txt
testout.pl
filelist.txt
simpledirtest.pl
dirread.pl

C:\My Documents\programs\perl\musicfix\test>perl -w dirread.pl
Please enter the name of the directory you want to list.
Be careful of capitalization.
: .
can’t opendir .
: No such file or directory at dirread.pl line 16, line 1.

C:\My Documents\programs\perl\musicfix\test>

Somebody, anybody, please tell me why this doesn’t work. I’m tearing my hair out here.

RSS feed

Comments

No comments yet.

Sorry, the comment form is closed at this time.