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

Opening multiple files in directory
opendir (DIR, '\srt') or die "Couldn't open directory, $!"; while ($file = readdir DIR) { chomp($file); open (my $f, '<', "\srt\$file") or die "Open failed : $!"; print "$file\n"; close $file; }

Replies are listed 'Best First'.
Re: why can't I
by Tux (Canon) on Oct 21, 2010 at 06:33 UTC

    I'm sure that you would have been warned with hints if you would have used use warnings;, as you are using windows-style folder path notation in combination with variable interpolation.

    "\srt\$file"

    will at least show you something like:

    Unrecognized escape \s passed through at test.pl line 12.

    Perl will translate unix-style path names to windows style path names when needed and necessary. Just use the correct slashes, and interpolation is no problem:

    Furthermore, a readdir () call does not read files like it was from the output of ls or DIR commands, so chomp'ing the names is just an extra attempt to make the file name illegal.

    Last, but not least, you open the file whose name is in $file, but the file handle is in $f (which would be more clear if you would use $fh), which should be used for the close ().

    use strict; use warnings; my $file; opendir DIR, "/srt" or die "Couldn't open directory, $!"; while ($file = readdir DIR) { open my $fh, "<", "/srt/$file" or die "Open failed : $!"; print "$file\n"; close $fh; } closedir DIR;

    Enjoy, Have FUN! H.Merijn
Re: why can't I
by ikegami (Patriarch) on Oct 21, 2010 at 06:25 UTC
    • readdir doesn't return newline-terminated lines. Using chomp is incorrect.
    • close $file is wrong since $file isn't a file handle.
    • You open the file named by $file, but you never read from the file.
Re: why can't I
by kcott (Archbishop) on Oct 21, 2010 at 06:07 UTC

    Some output would help.

    You need to close $f and closedir$fileDIR.

    Check the readdir documentation.

    Update:

    My apologies for any confusion I may have caused - see my correction above. The main point is open and close need to match and opendir and closedir need to match.

    -- Ken

Re: why can't I
by DrHyde (Prior) on Oct 21, 2010 at 09:43 UTC
    why can't I ... write a meaningful subject line
Re: why can't I
by TomDLux (Vicar) on Oct 21, 2010 at 15:02 UTC

    If you used the debugger, you would be able to investigate on your own any behaviour that doesn't match your expectations.

    A 15 second tutorial in how to use the debugger is available at Re: Debugger Tutorials.

    He also mentions using Perl as an interactive shell to experiment with commands. Strictly speaking, you invoke Perl as perl -d -e 0 --- perl with debugger, running the code consisting of the number zero ... in other words, nothing. I prefer the trick I learned from someone or another, invoking Perl as perl -demo --- perl with debugger, running the invalid code chunk 'mo'.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: why can't I
by dvd1999 (Initiate) on Oct 22, 2010 at 01:59 UTC
    thank you guys,especially to Tux. but I got a warning :permission denied when attempted to run the updated code. if i mute the open and close lines within the while loop, fine, DOS will prompt all the file names under srt directory. please!

      It will be easier to help if you post your modified code and the exact error message (cut and paste, not paraphrase).