Perl | Accessing a Directory using File Globbing
In Perl, a directory is used to store values in the form of lists. A directory is quite similar to a file. Just like a file, the directory also allows performing several operations on it. These operations are used for the modification of an existing directory or creation of a new one. A directory can be very easily opened and processed using the built-in function glob
.
Glob:
It returns a list of files that match the expression passed in the argument. This function can print all or the specific files whose extension has been passed to it.
Syntax:
@list = <*>; // Prints all files in current directory
@list = glob(“*.pl”); // Prints all files in current directory with extension .pl
@list = glob(‘//GeeksforGeeks//Files//*); // Prints all files in the given path
Here are some examples that illustrate accessing a directory using File Globbing
.
Examples:
Accessing the current directory of the script:
@files = glob ( '*' );
foreach $file ( @files )
{
print $file . "\n" ;
}
|
Output:
Opening a specified directory:
use File::Basename;
@files = glob ( 'C:/Users/GeeksForGeeks/Folder/*' );
foreach $file ( @files )
{
print basename( $file ), "\n" ;
}
|
Output:
Last Updated :
30 Jun, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...