Test2:...::File(3pm)
π Test2:...::File π
okay bestie, so this is a page from the User Contributed Perl Documentation section of the manual β the Test2:...::File(3pm) one, to be exact. pull up a chair, we're doing file stuff β¨
πΈ NAME πΈ
Test2::Harness::Util::File β a utility class for manipulating a file. that's it, that's the whole vibe π
π¦ DESCRIPTION π¦
so like, this is a utility class for file operations, right? but it's also the base class for several file helpers β so all those little helper classes are out here inheriting from it like it's the popular one at school. main character energy fr fr π
β¨ SYNOPSIS β¨
here's how you actually use it, no cap:
use Test2::Harness::Util::File;
my $f = Test2::Harness::Util::File->new(name => '/path/to/file');
$f->write($content);
my $fh = $f->open_file('<');
# Read, throw exception if it cannot read
my $content = $f->read();
# Try to read, but do not throw an exception if it cannot be read.
my $content_or_undef = $f->maybe_read();
my $line1 = $f->read_line();
my $line2 = $f->read_line();
...
π ATTRIBUTES π
$filename = $f->nameβ grabs the filename for you. it must also be provided during construction, so don't be out here trying to construct without it π$bool = $f->doneβ true ifread_line()has read every single line. you're at the end of the file, girl, no more lines left π«Ά
π₯ METHODS π₯
$decoded = $f->decode($encoded)β this one is a no-op, it just hands the argument right back at you unchanged, like "here, you dropped this". it gets called by"read"and"read_line". subclasses can override it if the file contains encoded data π$encoded = $f->encode($decoded)β also a no-op, returns the argument unchanged. this one is called by"write". subclasses can override this too if the file contains encoded data π$bool = $f->exists()β checks if the file exists. simple. iconic. we love a yes/no moment.$content = $f->maybe_read()β it'll read the file if it can and return the content (all lines joined together as a single string). if the file cannot be read, or does not exist, it returnsundef. no drama, no exception, just vibes π$fh = $f->open_file()$fh = $f->open_file($mode)β opens a handle to the file. if you don't pass$mode,<is used. default queen π$content = $f->read()β reads the file if it can and returns the content (all lines joined together as a single string). if the file cannot be read, or does not exist, this one throws an exception. so like, it's the dramatic sibling ofmaybe_read()π$line = $f->read_line()β reads a single line from the file. subsequent calls read the next line and so on until the end of the file is reached. reset it with thereset()method β¨$f->reset()β resets the internal line iterator thatread_line()uses. fresh start, new me, etc. π¦$f->write($content)β this is an atomic-write, bestie, pay attention. first$contentgets written to a temporary file using>mode. then the temporary file gets renamed to the desired file name, like a glow-up reveal. under the hood this useswrite_file_atomic()fromTest2::Harness::Util. slay π
lowkey the atomic-write thing is the whole point β nothing half-written ever shows up at the real filename. obsessed.
π SOURCE π
the source code repository for Test2-Harness lives at http://github.com/Test-More/Test2-Harness/. go look, it's giving open source π«Ά
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
π AUTHORS π
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright 2020 Chad Granum <exodist7@gmail.com>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
and that's the whole page, bestie π this was perl v5.40.1, dated 2026-05-10 β ily, go read your files responsibly, periodt β¨πΈ
π¦ where this page comes from
- ubuntu stonking: libtest2-harness-perl 1.000172-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms