Updated July 1, 2023
Introduction to Perl readdir
The Perl readdir is one of the functions that can be used to read the directory files, and the content of the data is checked and validated line by line. Also, it will return the next directory, which is already related to the same dir handle. The dir handle has the number of lists which is related to the directories created by the specific users, path, or location.
Syntax:
In a Perl script, the readdir() function is used to handle directory operations using a DIRHANDLE, which is obtained by opening a directory using the opendir() function.
#!/usr/bin/perl -w
$var="";
loops $var name= readdir(DIR)
—some Perl script logic codes based upon the user requirements—
The above codes are the basic syntax for the readdir() method in the Perl script. Whenever we used the readdir() method before, we just used the opendir(DIR, $var name).
How readdir function work in Perl?
The readdir() function will handle all the directories the users create. When an error occurs, the Perl script can utilize the die keyword to send notifications to the application console or the user end. All the directory functions are operated using the same symbols like <> open and close symbols. It will be handled using some directory handlers. Sometimes the <> this symbol is not used in the directory handler, which is related to the readdir function. Actually, the readdir() function returns the next directory files by using the scalar contexts.
Examples
Let us discuss examples of Perl readdir.
Example #1
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $vars = 'E:\\yt';
opendir(DIR, $vars) or die $!;
while (my $vars1 = readdir(DIR)) {
next if ($vars1 =~ m/^\./);
print "Welcome To My Domain \n $vars1\n";
}
closedir(DIR);
my @vars2 = glob("*.pl *.jpg");
foreach my $vars1 (@vars2) {
print "Have a Nice Day User \n $vars1\n";
}
exit 0;
Output:
In the above example, we used the readdir() function in the loops, like the while loop. Mainly the while loop first checks the condition. If the condition is true, it will execute the loop statements. In that while loop have the arguments called the DIR in the variables using the $ symbol using the next if condition method taking the variables as the arguments. Using regular expressions like /, ^,\, these are the operators passing to the while loop.
Example #2
Code:
#!/usr/bin/perl -w
use strict;
use File::Slurp;
my $vars = "E:/issues";
my $vars1;
opendir ( DIR, $vars ) || die "Error in opening dir $vars\n";
while( ($vars1 = readdir(DIR))) {
print("Welcome User your directory files are listed please find it \n $vars1\n");
}
my @vars2=read_dir($vars);
my $vars3=@vars2;
for(my $i=0;$i<$vars3;$i++){
print"Thanks Users your directory files are listed below \n FileList: $vars2[$i]\n";
}
Output:
In the Second Example, we used readdir() method in different ways. We have initialized and declared the variable with the values as location or directory path in the file. When we use a while loop, it first checks the condition. If the condition is true, then it will execute the statements using opendir().
Example #3
Code:
#!/usr/bin/perl -w
opendir(DIR, ".");
@var = grep(/\.txt$/,readdir(DIR));
closedir(DIR);
foreach $vars1 (@var) {
print "Welcome To my Domain the specific text files are listed which is stored in the specific directory thanks $vars1\n";
}
Output:
We used the specific format like .txt by using the grep Linux commands; it will fetch and retrieve the specific format files on the console. Generally, the grep command is the most advanced and frequent command for searching the specific characters in the string or else finding the specific character in the big file or space in the input.
Conclusion
In conclusion, the Perl readdir() function reads files within a specific directory, as specified in the input. Utilizing the readdir() function eliminates a manual search, resulting in time-saving automation.
Recommended Articles
This is a guide to Perl readdir. Here we discuss the Introduction and How readdir function works in Perl and examples with code implementation. You may also have a look at the following articles to learn more –