Perl File Operations Guide — File::Basename, File::Copy, and Path Management
In this tutorial, you will learn about Perl File Operations Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Perl file operations extend basic I/O with File::Basename for path components, File::Copy for file copying, File::Path for directory creation/deletion, File::Find for recursive directory traversal, and glob for wildcard filename expansion.
File::Basename
use File::Basename;
my $path = "/home/user/docs/report.pdf";
my $name = basename($path); # "report.pdf"
my $dir = dirname($path); # "/home/user/docs"
my ($base, $dir, $ext) = fileparse($path, qr/\.[^.]*$/);
# base="report", dir="/home/user/docs/", ext=".pdf"
File::Copy
use File::Copy;
# Copy file
copy("source.txt", "dest.txt") or die "Copy failed: $!";
# Move file
move("old.txt", "new.txt") or die "Move failed: $!";
# Copy with permissions
copy("source.txt", "dest.txt")
or die "Copy failed: $!";
chmod((stat("source.txt"))[2], "dest.txt"); # preserve perms
File::Path
use File::Path qw(make_path remove_tree);
# Create directory (like mkdir -p)
make_path("/tmp/a/b/c") or die "mkdir failed: $!";
# Remove directory tree (like rm -rf)
remove_tree("/tmp/a/b") or die "rm failed: $!";
# Create with permissions
make_path("/tmp/data", { mode => 0755 });
File::Find
use File::Find;
my @files;
find(sub {
return unless -f $_;
push @files, $File::Find::name;
}, "/tmp");
# Find by extension
my @txt_files;
find(sub {
push @txt_files, $File::Find::name if /\.txt$/;
}, ".");
Common Mistakes
1. Not checking return values
All file operations return false on failure. Check with or die "Error: $!".
2. File::Find modifies $_
$_ is aliased to the current filename. Localize $_ if you modify it in the wanted subroutine.
3. File::Copy doesn't preserve attributes
File::Copy copies content only. Permissions, ownership, and timestamps must be copied separately.
Practice Questions
1. How do you get the filename from a path?
basename($path) from File::Basename returns the filename portion of a path.
2. How do you copy a file in Perl?
use File::Copy; copy("source", "dest") copies the file content.
3. How do you recursively list files?
use File::Find; find(\&wanted, $dir) traverses directories recursively.
FAQ
{{< faq question="How do I create directories recursively?" >}}
make_path("/a/b/c/d") from File::Path creates all parent directories as needed.
{{< /faq >}}
{{< faq question="What is the difference between glob and opendir?" >}}
glob uses wildcard patterns: my @files = glob("*.txt"). opendir/readdir provides more control.
{{< /faq >}}
{{< faq question="How do I get file modification time?" >}}
(stat($file))[9] returns mtime. Use -M $file for days since modification or my $mtime = (stat($file))[9].
{{< /faq >}}
What's Next
Now learn about memory management in Perl.
| Topic | Description | Link |
|---|---|---|
| Memory | Memory management and leaks | {{< ref "30-memory" >}} |
| Python | Compare with Python | Python |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro