How to list directory contents with Path::Tiny in Perl?
Question
How to list directory contents with Path::Tiny in Perl?
Listing Directory Contents with Path::Tiny in Perl
Path::Tiny is a lightweight and convenient Perl module for file and directory path manipulation—an excellent alternative to using core functions like opendir, readdir, or File::Find. It provides an easy-to-use object-oriented interface that makes directory traversal clean and readable.
To list the contents of a directory using Path::Tiny, follow these simple steps:
- Create a
Path::Tinyobject pointing to your target directory. - Call the
childrenmethod on the directory object. This returns a list ofPath::Tinyobjects representing the directory entries (files, subdirectories, etc.). - Iterate over the returned list and, for example, print their names or paths.
Key Concepts in the Example
Path::Tiny->new($dir)creates a path object (note the use of arrow->for method calls on classes/objects).childrenreturns the directory contents in list context, and the entries arePath::Tinyobjects themselves.- You can call
basename,stringify, or just stringify the object to get a string representation of the path. - Perl context rule: method like
childrenreturn a list in list context (so you can iterate withforeach).
Example: Listing All Entries in the Current Directory
use strict;
use warnings;
use Path::Tiny;
# Create a Path::Tiny object for the current directory
my $dir = Path::Tiny->new(".");
# Get all entries (files and directories)
my @entries = $dir->children;
print "Directory contents of '", $dir->stringify, "':\n";
foreach my $entry (@entries) {
# $entry is a Path::Tiny object; use basename for just the name
print "- ", $entry->basename, "\n";
}
Additional Tips and Gotchas
childrenby default does not recurse. It only lists immediate files/folders inside the directory.- It returns all entries except
.and... - You can filter entries by passing a code reference to
children, e.g.$dir->children(sub { $_->is_file })to get only files (Perl 5.10+). - Since
Path::Tinyobjects overload stringification, simply printing them gives the path as a string. - Make sure
Path::Tinymodule is installed (`cpan Path::Tiny`) as it's not core.
Summary
Path::Tiny makes directory listing straightforward and readable by providing the children method that returns directory items as objects. This improves readability over traditional opendir/readdir and helps avoid common pitfalls such as forgetting to exclude . and ...
Use the simple example above to get started listing directories with Path::Tiny, then explore filtering and recursion for advanced traversal.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 19ms
Directory contents of '.':
- .bun-cache
- database
- tooling
- example_copy.txt
- .DS_Store
- frontend
- perl-qa-batch1.json
- Makefile
- gemini-perl.json
- markz_status.txt
- output.json
- filename.txt
- example_source.txt
- EXPR
- backend
- docs
- README.md
- .env
- processed_profile.txt
- scripts
- example.txt
- advanced-perl-challenges.json
- api
- docker-compose.yml
- perl-qa-batch2.json
- .tmp
- filename
- .env.example
- AGENTS.md
- test.db
- data
- front
- memory:
- output.txt
- CLAUDE.md
- perl-qa-batch3.json
(empty)Was this helpful?
Related Questions
- How to use File::Slurp to read entire file into variable in Perl?
- How to get file modification time with Path::Tiny in Perl?
- How to use Path::Tiny touch to create empty file in Perl?
- How to use List::Util reduce for custom aggregation in Perl?
- How to compare two DateTime objects in Perl?
- How to use DateTime::Duration for time periods in Perl?