Open In App

Perl | glob() Function

glob() function in Perl is used to print the files present in a directory passed to it as an argument. This function can print all or the specific files whose extension has been passed to it.

Syntax: glob(Directory_name/File_type);



Parameter: path of the directory of which files are to be printed.

Returns: the list of files present in the given directory



Example 1: Printing names of all the files in the directory




#!/usr/bin/perl
  
# To store the files
# from the directory in the array
@files = glob('C:/Users/GeeksForGeeks/Folder/*');
  
# Printing the created array
print "@files\n";

Output:


Above example will print all the files of the requested directory.

Example 2: Printing names of the specific files in the directory




#!/usr/bin/perl
  
# To store the files of a specific extension
# from the directory in the array
@Array = glob('C:/Users/GeeksForGeeks/Folder/*.txt');
  
# Printing the created array
print "@Array\n";

Output:


Above example will print all the files of the requested directory that end with .txt extension.

Article Tags :