cpan beginner

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::Tiny object pointing to your target directory.
  • Call the children method on the directory object. This returns a list of Path::Tiny objects 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).
  • children returns the directory contents in list context, and the entries are Path::Tiny objects themselves.
  • You can call basename, stringify, or just stringify the object to get a string representation of the path.
  • Perl context rule: method like children return a list in list context (so you can iterate with foreach).

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

  • children by 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::Tiny objects overload stringification, simply printing them gives the path as a string.
  • Make sure Path::Tiny module 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

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
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
STDERR
(empty)

Was this helpful?

Related Questions