note
vinoth.ree
<b>Opening the directory</b>
<p>To open the directory,use a function called "opendir". You use this same like the open function to open files. Below example shows opening of /tmp directory. </p>
<code>
#!/usr/bin/perl
use strict;
use warnings;
my $tmp_dir="/tmp";
opendir (DIR, $tmp_dir) or die $!;
</code>
<b>Reading the directory</b>
<p>To read the files and directories in the directory we use the readdir function. readdir returns the name of each file or directory in the opened directory in turn when used in scalar context, or a list of the names of all files and directories in that directory when used in list context. This means that we can use readdir in a foreach loop or any other loop</p>
<code>
while (my $file_name = readdir(DIR)) {print "$file_name\n";}
</code>
<b>Closing the directory</b>
<p>
We use the function closedir to close the directory once we are finished with it. Like files, the directory will be closed when the program finish, but sometimes need to close the directory opened
<code>
closedir(DIR);
</code>
1012201
1012201