Directories in Perl

Here’s the translation of the Go code to Perl, with explanations adapted for Perl:

Our first program will demonstrate working with directories in Perl. Here’s the full source code with explanations:

#!/usr/bin/env perl

use strict;
use warnings;
use File::Path qw(make_path remove_tree);
use File::Spec;
use Cwd;

# Helper function to check for errors
sub check {
    my $err = shift;
    die $err if $err;
}

# Create a new sub-directory in the current working directory
mkdir "subdir" or check($!);

# When creating temporary directories, it's good practice to remove them afterwards
END { remove_tree("subdir") }

# Helper function to create a new empty file
sub create_empty_file {
    my $name = shift;
    open my $fh, '>', $name or check($!);
    close $fh;
}

create_empty_file("subdir/file1");

# We can create a hierarchy of directories, including parents with make_path
# This is similar to the command-line `mkdir -p`
make_path("subdir/parent/child") or check($!);

create_empty_file("subdir/parent/file2");
create_empty_file("subdir/parent/file3");
create_empty_file("subdir/parent/child/file4");

# opendir and readdir list directory contents
opendir(my $dh, "subdir/parent") or check($!);
my @entries = readdir($dh);
closedir($dh);

print "Listing subdir/parent\n";
for my $entry (@entries) {
    next if $entry eq '.' or $entry eq '..';
    my $is_dir = -d "subdir/parent/$entry" ? 'true' : 'false';
    print " $entry $is_dir\n";
}

# chdir lets us change the current working directory, similarly to `cd`
chdir("subdir/parent/child") or check($!);

# Now we'll see the contents of `subdir/parent/child` when listing the current directory
opendir($dh, ".") or check($!);
@entries = readdir($dh);
closedir($dh);

print "Listing subdir/parent/child\n";
for my $entry (@entries) {
    next if $entry eq '.' or $entry eq '..';
    my $is_dir = -d $entry ? 'true' : 'false';
    print " $entry $is_dir\n";
}

# `cd` back to where we started
chdir("../../..") or check($!);

# We can also visit a directory recursively, including all its sub-directories
print "Visiting subdir\n";
visit("subdir");

sub visit {
    my $dir = shift;
    opendir(my $dh, $dir) or check($!);
    while (my $entry = readdir($dh)) {
        next if $entry eq '.' or $entry eq '..';
        my $path = File::Spec->catfile($dir, $entry);
        my $is_dir = -d $path ? 'true' : 'false';
        print " $path $is_dir\n";
        visit($path) if -d $path;
    }
    closedir($dh);
}

To run the program, save it as directories.pl and use:

$ perl directories.pl
Listing subdir/parent
 child true
 file2 false
 file3 false
Listing subdir/parent/child
 file4 false
Visiting subdir
 subdir true
 subdir/file1 false
 subdir/parent true
 subdir/parent/child true
 subdir/parent/child/file4 false
 subdir/parent/file2 false
 subdir/parent/file3 false

This Perl script demonstrates various operations with directories, including creating directories, listing their contents, changing the current working directory, and recursively visiting a directory tree. It uses Perl’s built-in functions and modules like File::Path and File::Spec to handle these operations in a cross-platform manner.