perl function glob
The glob
function is a built-in Perl function that is used to expand filenames using shell-like globbing patterns. It takes a pattern as its argument and returns a list of filenames that match the pattern.
Here's an example that demonstrates how to use glob
:
#!/usr/bin/perl use strict; use warnings; # Get a list of all files in the current directory my @files = glob("*"); # Print the list of files foreach my $file (@files) { print "$file\n"; }
In this example, we use the globbing pattern *
to match all files in the current directory. We pass the pattern to the glob
function, which returns a list of filenames that match the pattern. We assign the list to the variable @files
and then print each filename to the console using a foreach
loop.
Here's another example that demonstrates how to use more complex globbing patterns:
#!/usr/bin/perl use strict; use warnings; # Get a list of all text files in the current directory and its subdirectories my @files = glob("**/*.txt"); # Print the list of files foreach my $file (@files) { print "$file\n"; }
In this example, we use the globbing pattern **/*.txt
to match all text files in the current directory and its subdirectories. The **
wildcard matches any number of subdirectories, and the *.txt
wildcard matches any file with a .txt
extension. We pass the pattern to the glob
function, which returns a list of filenames that match the pattern. We assign the list to the variable @files
and then print each filename to the console using a foreach
loop.