Based on what I read from the the python docs, the following wouldn't be equivalent, but it would be close:
do {
opendir(my $dh, $path)
or die("Can't open directory \"$path\": $!\n");
my @files = grep !/^\.\.?\z/, readdir($dh);
closedir($dh)
or die("Error reading directory \"$path\": $!\n");
@files
}
Some differences:
- It's not really possible to check if
readdir
fails.
- The error message thrown is different.
- The thrown exception wouldn't be of the non-existent OSError class.
- The exception enclosed in a Python OSError class may not be available to whoever catches the Perl exception.
- More? I don't know Python.
It would have been better if you had specified what you actually wanted to do rather saying you want the equivalent of some other language's function, since
- The chances of two languages having exactly the same function is not that good, so you leave us guessing what you actually want.
- You could actually have searched for that.