testing intermediate

How to test file operations with Test::File in Perl?

Question

How to test file operations with Test::File in Perl?

Testing File Operations in Perl Without Test::File

The Test::File module simplifies testing files, but since it’s not a core module, it isn’t available in sandboxed environments without CPAN access. Fortunately, Perl's core modules like Test::More combined with built-in file tests provide a reliable way to test file operations in any environment.

Common Perl Concepts Used

  • -e and -s file tests: check file existence and size.
  • Filehandles with open and explicit read/write modes (>, <).
  • Slurp mode with local $/ = undef; to read whole file contents at once.
  • stat function to inspect file permissions.
  • Test::More for structured test assertions (ok, is).

Corrected Example: Test File Creation, Content, and Permissions

use strict;
use warnings;
use Test::More tests => 5;

my $filename = "testfile.txt";

# Create and write content
open my $fh, '>', $filename or die "Cannot open '$filename': $!";
print $fh "Hello, core testing!\nThis is a sample file.";
close $fh;

# Test if file exists
ok(-e $filename, "File created successfully");

# Read entire file content using slurp mode
open my $rfh, '<', $filename or die "Cannot open '$filename' for reading: $!";
local $/ = undef;
my $content = <$rfh>;
close $rfh;

# Exact content match test
is($content, "Hello, core testing!\nThis is a sample file.", "File content matches exactly");

# Regex test on file content
ok($content =~ /core testing!/, "File content matches regex");

# File is not empty
ok(-s $filename > 0, "File is not empty");

# Check file permission bits for owner read/write (platform-dependent)
my $mode = (stat($filename))[2] & 07777;  # extract permission bits
ok(($mode & 0600) == 0600, sprintf("File permissions have rw------- bits (mode: %04o)", $mode));

# Clean up
unlink $filename or warn "Could not delete $filename: $!";

Explanation

This script demonstrates how to:

  • Create a file and write a known string.
  • Assert file existence with the -e test.
  • Read the whole file with slurp mode for exact content comparison using is.
  • Validate a regex match against file content with ok.
  • Verify the file is not empty via -s.
  • Check basic permission bits using stat (note: permission bits vary by OS).
  • Clean up the test file afterward to avoid clutter.

Common Pitfalls

  • Quoting in open calls: Always use single quotes inside the double-quoted error messages '$filename' to prevent syntax errors.
  • Remember to local $/ = undef; before reading a file to enable slurp mode (reads the whole file instead of just one line).
  • File permission checks can be unreliable on Windows; test them primarily on UNIX-like systems.
  • Clean up test files to prevent interference with subsequent runs.
  • Test output lines should correspond exactly to the planned test count (tests => 5 here).

Summary

In restricted sandboxes without Test::File, use core Perl tools like Test::More, explicit filehandles, and built-in file tests to verify file operations reliably. This approach requires no external dependencies, runs quickly, and is fully compatible with Perl 5 core.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 67ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
1..5
ok 1 - File created successfully
ok 2 - File content matches exactly
ok 3 - File content matches regex
ok 4 - File is not empty
ok 5 - File permissions have rw------- bits (mode: 0644)
STDERR
(empty)

Was this helpful?

Related Questions