Updated April 17, 2023
Introduction to Perl glob
Whenever we want to print the names of the files given in a specific directory in Perl, we make use of a function called glob() function in Perl. It takes the parameter as a path to the directory where the files are present whose names are to be returned as the output by the glob() function. This glob() function can either print the names of all the files present in the directory or can print the name of the specific file present in the directory depending on what we specify in the path to the directory where the files are present as the parameter whose names are to be returned as output by the glob() function.
Syntax to Declare glob() Function in Perl:
glob(path_to_the_directory);
Where path_to_the_directory is the path to the directory where the files are present whose names are to be returned as the output by the glob() function.
Working of glob() Function in Perl
- Whenever we want to print the names of the files given in a specific directory in Perl, we make use of a function called glob() function in Perl.
- The glob() function takes the parameter as a path to the directory where the files are present whose names are to be returned as the output by the glob() function.
- The glob() function can either print the names of all the files present in the directory or can print the name of the specific file present in the directory depending on what we specify in the path to the directory where the files are present as the parameter whose names are to be returned as output by the glob() function.
Examples of Perl glob
Given below are the examples of Perl glob:
Example #1
Perl program to print the names of the files present in the directory specified by making use of glob() function.
Code:
#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob('C:/Users/admin/Desktop/*');
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}
Output:
In the above program, we are making use of the glob() function by passing the path of the directory where the files are present to print the names of the files present in the directory. Then the file names present in the directory are printed as the output on the screen.
Example #2
Perl program to print the names of the files present in the directory specified by making use of glob() function.
Code:
#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob('C:/Users/admin/Desktop/EDUCBA/*');
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}
Output:
In the above program, we are making use of the glob() function by passing the path of the directory where the files are present to print the names of the files present in the directory. Then the file names present in the directory are printed as the output on the screen.
Example #3
Perl program to print the names of the files present in the directory specified by making use of glob() function.
Code:
#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob(E:/Images/*');
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}
Output:
In the above program, we are making use of the glob() function by passing the path of the directory where the files are present to print the names of the files present in the directory. Then the file names present in the directory are printed as the output on the screen.
Example #4
Perl program to print the names of the files present in the directory specified by making use of glob() function.
Code:
#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob(F:/Music/*');
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}
Output:
In the above program, we are making use of the glob() function by passing the path of the directory where the files are present to print the names of the files present in the directory. Then the file names present in the directory are printed as the output on the screen.
Example #5
Perl program to print the names of the files present in the directory specified by making use of glob() function.
Code:
#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob(D:/WindowsUpdate/*');
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}
Output:
In the above program, we are making use of the glob() function by passing the path of the directory where the files are present to print the names of the files present in the directory. Then the file names present in the directory are printed as the output on the screen.
Recommended Articles
This is a guide to Perl glob. Here we discuss the introduction, working of glob() function in Perl along with examples, respectively. You may also have a look at the following articles to learn more –