perl function readdir

w‮figi.ww‬tidea.com

The readdir function in Perl reads the contents of a directory and returns a list of all files and directories in it.

Here is an example that reads the current directory and prints out all the files and directories it contains:

opendir(DIR, ".");
while (my $file = readdir(DIR)) {
  print "$file\n";
}
closedir(DIR);

In the above example, opendir opens the current directory (.) and returns a directory handle that is assigned to the variable DIR. The readdir function is then called in a while loop to read each file and directory in the directory handle. Each file and directory name is assigned to the variable $file and is printed out using the print function. Finally, closedir closes the directory handle.