file-io intermediate

How to use glob to get files matching a pattern in Perl?

Question

How to use glob to get files matching a pattern in Perl?

The glob function in Perl is a powerful and simple way to retrieve a list of filenames matching a wildcard pattern, similar to shell wildcard expansion (glob patterns). It allows you to match files using patterns such as * (any characters), ? (any single character), and ranges like [a-z].

How glob works in Perl

glob takes a string pattern and returns a list of filenames matching that pattern in the current directory (or a specified directory). It behaves somewhat like shell globbing but is implemented internally by Perl. The returned list is sorted (usually alphabetically).

Example patterns:

  • *.txt — all files ending with .txt
  • file?.pl — files like file1.pl, fileA.pl
  • data_{1,2,3}.csv — files data_1.csv, data_2.csv, data_3.csv

Using glob in scalar and list context

glob behaves differently depending on context:

  • List context: Returns a list of all filenames matching the pattern.
  • Scalar context: Returns the next filename on each call, cycling through the list. This can be useful for iterators.

Simple example to get files matching a pattern

This example prints all files in the current directory ending with .pm (Perl modules):


use strict;
use warnings;

my @pm_files = glob("*.pm");

print "Found Perl module files:\n";
foreach my $file (@pm_files) {
    print "$file\n";
}

More complex pattern example

You can also combine patterns using braces (supported since Perl 5.6):


use strict;
use warnings;

# Match .pl and .pm files
my @perl_files = glob("{*.pl,*.pm}");

print "Perl scripts and modules found:\n";
print "$_\n" for @perl_files;

Important points and gotchas

  • glob operates relative to the current working directory unless your pattern specifies absolute paths.
  • glob does not recurse into subdirectories by default; you need additional code or modules for recursive globbing.
  • Patterns must be passed as double-quoted strings or barewords. Single quotes will prevent interpolation of special characters.
  • glob returns an empty list if no files match — always check to avoid surprises.
  • glob can be used as a scalar iterator (e.g. while (my $file = glob("*.txt")) { ... }), which can be efficient in some cases.
  • On Windows, the exact behavior may vary due to differences in filesystem and shell conventions.

Summary

In Perl, glob provides the easiest way to get files matching shell-style wildcard patterns. It’s simple: just call glob(“pattern”) in list context to get all matching filenames. Remember its behavior with context and pattern syntax for best results.

Runnable example

The code below lists all .pm and .pl files in the current directory, printing each found filename:


use strict;
use warnings;

my @files = glob("{*.pm,*.pl}");

if (@files) {
    print "Matching files found:\n";
    print "$_\n" for @files;
} else {
    print "No matching files found.\n";
}

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 9ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Found Perl module files:
STDERR
(empty)

Was this helpful?

Related Questions