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

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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";

chevron_right


Output:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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";

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.