Directories and paths
List a directory
opendir and readdir walk a directory the way open and readline walk a file. The entries arrive in filesystem order, so sort them before you show them to a person.
opendir my $dh, "/var/log" or die "opendir: $!\n"; my @entries = sort readdir $dh; closedir $dh; print join " ", grep { !/^\.\.?$/ } @entries; print "\n";
perl demo.pl
apk.log
Every directory contains the entries dot and dot-dot, and almost every script filters them out. The grep above drops exactly those two names and nothing else. The /var/log of this container holds a single file, apk.log; your own system prints its own list here.
glob
glob expands a shell pattern in one call, which is shorter than opendir plus a filter when the pattern is the point. It understands stars, question marks and character classes.
my @logs = glob("/var/log/*.log"); my @confs = <*.conf>; print "$_\n" for @logs;
perl demo.pl
/var/log/apk.log
The angle-bracket form and the function do the same thing. Prefer glob() in new code: it is a normal function call and never confused with the readline operator. The second line collects the .conf files of the current directory into @confs and prints nothing, because the directory you run from has none.
Parse and build paths
Do not split paths on slashes yourself. File::Basename knows about trailing separators, and File::Spec joins parts with the separator of the current system, which keeps scripts working on Windows.
use File::Basename qw(basename dirname); use File::Spec; my $full = "/var/log/app/app.log"; print basename($full), "\n"; # app.log print dirname($full), "\n"; # /var/log/app print File::Spec->catfile("/tmp", "out", "r.txt"), "\n";
perl demo.pl
app.log
/var/log/app
/tmp/out/r.txt
Both modules ship with Perl, so the import costs nothing. The same family provides fileparse for splitting a name into path, base and suffix.
Create and remove trees
File::Path creates a whole missing chain with make_path and removes one with remove_tree. Both return what they created or removed, and both die on a real failure such as an unwritable parent, unless you pass error => \my $err to collect the problems instead.
use File::Path qw(make_path remove_tree); make_path("/tmp/report/2026"); print "ready\n" if -d "/tmp/report/2026"; remove_tree("/tmp/report/2026");
perl demo.pl
ready
Path::Tiny is the modern alternative that wraps all of this in one small object, and it is the module most new Perl code uses. Lesson 6.6 explains how to install it from CPAN.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
In what order does
readdirreturn the entries of a directory?Answer
In filesystem order, so sort them before showing them to a person
The order is whatever the directory happens to have, which is why every example sorts the result.
-
Which two entries does every directory contain?
Answer
The dot and the dot-dot entries
The
grep { !/^\.\.?$/ }filter drops exactly those two names and nothing else. -
What is the difference between
glob("/var/log/*.log")and<*.conf>?Answer
Both expand a shell pattern;
glob()is a normal function call and is preferred in new codeThe angle-bracket form is easy to confuse with the readline operator, so the function is the clearer choice.
-
Why use File::Basename instead of splitting a path on slashes?
Answer
It knows about trailing separators and the difference between the path and the name
A path has edge cases a plain split does not handle, and the module ships with Perl.
-
What does
File::Spec->catfile("/tmp", "out", "r.txt")build?Answer
The path /tmp/out/r.txt with the separator of the current system
File::Spec joins the parts with the separator the system uses, which keeps scripts working on Windows.
-
Which call creates a whole missing chain of directories at once?
Answer
File::Path::make_pathmake_pathcreates every missing level, whilemkdirmakes one directory and needs the parent to exist. -
What does this program print?
mkdir "newdir"; print -d "newdir" ? "dir\n" : "no dir\n"; rmdir "newdir";Answer
dir
The
mkdircreates the directory in the working folder, and-dis true for it. -
What does this program print?
use File::Basename qw(basename); print basename("/var/log/app.log"), "\n";Answer
app.log
basenamereturns the last part of the path, which is the file name with its extension. -
What does this program print?
use File::Spec; print File::Spec->catfile("a", "b.txt"), "\n";Answer
a/b.txt
The parts are joined with a single separator, so the result is one path.
-
What does this program print?
my @list = ("b", ".", "a"); print join(" ", sort grep { !/^\.\.?$/ } @list), "\n";Answer
a b
The filter drops the dot entry and the sort puts a before b.
Article author: Arthur Isaev