Introduction to Perl readdir
The Perl readdir is one of the function that can be used to read the directory files and the content of the data are checked and validated in 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. Once the directory is opened by using the opendir() method with the help of loops the directory files are iterated and the values are to be evaluated using the readdir() method.
Syntax:
In Perl script document the Perl readdir() is one of the method that will be handled by the DIRHANDLE by using the user path or location. the directory is identified and opened by using the open() method and check the data values in the directory files by using readdir() method.
#!/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 readdir() method before that we must used the opendir(DIR, $var name) the DIR is the argument that is going to be used for the next directory fetched by the DIRHANDLE lists.
How readdir function work in Perl?
The readdir() function will used for handle all the directories which is created by the users the directories are stored and retrieved by the DIRHANDLE. By using the opendir() method the directories are opened and also the opendir() itself they pass the arguments which is related to the next directory queues. By using the die keyword, it will sent the notification for the application console in case of error which is received by the user end. If we want to list the files in the directories also the files which is matching with the certain patterns like regular expressions etc or with the certain ending operators and symbols represented in the directory. All the directory functions are operated using the some 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 the next directory is reached when it can be returned using the some keywords like undef, def etc. The $ symbol is used for to identify the variables, identifiers for the mentioned current and new directories list.
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:
The above example we used the readdir() function in the loops like while loop. Mainly the while loop first checks the condition if the condition is true it will executed the loop statements. In that while loop have the arguments called the DIR in the variables using $ symbol using next if condition method taking the variables as the arguments. Using some regular expressions like /, ^,\ these are the operators which is passing to the while loop. Using the @vars2 variable it initialise and calls the glob method it can take the .pl and .jpg has the arguments using foreach the variables are initialise and calls the argument as new variables which is related to the file search directories.
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 Second Example we used readdir() method in different ways. We have initialised and declare the variable with the values as location or directory path in the file. By using the for loop the values are iterated and the directory folder files are listed in the console. When we use while loop it first checks the condition if the condition is true then it will execute the statements using opendir() the directory is opened using read_dir the directory files are listed and validated using the input extensions like .pl, etc. If suppose we would not mention any file format it will retrieve and displayed all the files which is mentioned in the specific directory of the folders.
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:
The final example we used readdir and opendir are the two Perl functions which is used for the specific directories. We have not declared any specific directory in the variables without mentioning that the directories are listed in the folders, files on the output console. 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 commands for searching the specific characters in the string or else finding the specific character in the big file or space in the input. Here we will use the same grep command that will be used to list the specific format files on the mentioned directory folders.
Conclusion
In concluded part the Perl readdir() function is used for read the directory files which has been mentioned in the specific directory in the input also without mentioning the directory names the script will be executed based on the script file save location. So when we use the readdir function the manual search is avoided the time will be save by using this automation script.
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 –
6 Online Courses | 4 Hands-on Projects | 38+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses