Syntax
Different syntaxes of the glob() function are mentioned below.
- glob(directory_path)
The above glob syntax is used to search the content of a particular directory path that will be mentioned in the argument. - glob expr;
The above glob syntax is used to search the content of the directory based on the matching pattern.
The glob() function returns the list of files and folders of the particular directory.
Metacharacters
The following metacharacters are commonly used for writing regular expression patterns in PERL
Character | Description |
---|---|
. | It is used to match any character without a newline(‘\n’). |
* | It is used to match any character zero or more times. |
? | It is used to match any character for zero or one time. |
() | It is used for grouping. |
[..] | It is used for defining the character class. |
~ | It is used to match with the home directory. |
{..} | It is used to match comma-separated words enclosed by the second brackets. |
Different examples of the glob() function have been shown below.
Example-1: Retrieve All Files and Folders of a Particular Directory
Create a PERL file with the following code to know the use of the glob() function for searching all files and folders of the particular directory. According to the directory path defined in the code, all files and folders of the path, “/home/Fahmida/*,” will be stored in an array if the path exists; otherwise, an error message will appear. Next, the content of the array will be printed.
@content = glob('/home/fahmida/*');
#Print the value returned by the glob() function
print "@content\n";
Output:
The list of all files and folders of the directory, “home/fahmida/,” has been displayed in the output.
Example-2: Retrieve the Specific Files of a Particular Directory
Create a PERL file with the following code to know the use of the glob() function for searching the list of specific files inside a particular directory. According to the directory path defined in the code, all PERL files of the path, “/home/Fahmida/*,” will be stored in an array if the path exists; otherwise, an error message will appear. Next, the content of the array will be printed.
@content = glob('/home/fahmida/*.pl');
#Print the value returned by the glob() function
print "@content\n";
Output:
The list of all text files of the directory, “home/fahmida/,” has been displayed in the output.
Example-3: Retrieve All Files and Folders of the Current Directory
Create a PERL file with the following code to know the use of the glob() function for searching all files and folders of the current directory. All files and folders of the current directory will be stored in an array. Next, the content of the array will be printed.
@content = glob('*');
#Iterate the returned value using loop
foreach $val (@content)
{
#Print each value
print $val . " ";
}
#Add newline
print "\n";
Output:
The following output shows all files and folders of the current directory.
Example-4: Retrieve the Specific Files of the Current Directory
Create a PERL file with the following code to know the use of the glob() function for searching all text files of the current directory. All text files of the current directory will be stored in an array. Next, the content of the array will be printed.
@content = glob "*.txt";
#Iterate the returned value using loop
foreach $val (@content)
{
#Print each value
print $val . " ";
}
#Add newline
print "\n";
Output:
The following output shows all text files of the current directory.
Example-5: Retrieve the Specific Folders Using “?”
Create a PERL file with the following code to know the use of the glob() function for searching particular folders of the current directory by using “?”. All folders of the current directory that start with the character “t” and the folder name containing 4 characters will be stored in an array. Next, the content of the array will be printed.
# that starts with 't' and contains 4 characters
@content = glob ("t???");
#Iterate the returned value using the loop
foreach $val (@content)
{
#Print each value
print $val . " ";
}
#Add newline
print "\n";
Output:
The following output shows all folders of the current directory that starts with the character “t,” and the folder name contains 4 characters.
Example-6: Retrieve the Specific Folders Using “*” and [0-9]
Create a PERL file with the following code to know the use of the glob() function for searching particular folders of the current directory by using “*” and [0-9]. All files of the current directory that start with any character but end with “ob” and a digit will be stored in an array. Next, the content of the array will be printed.
@content = glob ("*ob[0-9].*");
#Iterate the returned value using loop
foreach $val (@content)
{
#Print each value
print $val . " ";
}
#Add newline
print "\n";
Output:
The following output shows that the current directory contains 6 files that match the defined pattern of the code.
Conclusion
The way of searching files and folders in a particular location by using the glob() function has been shown here. The uses of different types of metacharacter to write the searching pattern have been shown in the examples.