Test2::Harness::Util::File3pm
Utility class for manipulating a file.
Usage
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 readmy $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();...
Overview
This is a utility class for file operations. This also serves as a base class for several file helpers.
Attributes
$filename = $f->name;
|
Get the filename. Must also be provided during construction. |
|---|---|
$bool = $f->done;
|
True if |
Methods
- $decoded = $f->decode($encoded)
-
This is a no-op, it returns the argument unchanged. This is called by
"read"and"read_line". Subclasses can override this if the file contains encoded data.
- $encoded = $f->encode($decoded)
-
This is a no-op, it returns the argument unchanged. This is called by
"write". Subclasses can override this if the file contains encoded data.
- $bool = $f->
exists() -
Check if the file exists
- $content = $f->
maybe_read() -
This will 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 this will return undef.
- $fh = $f->
open_file()
- $fh = $f->open_file($mode)
-
Open a handle to the file. If no
$modeis provided'<'is used.
- $content = $f->
read() -
This will 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 this will throw an exception.
- $line = $f->
read_line() -
Read a single line from the file, subsequent calls will read the next line and so on until the end of the file is reached. Reset with the
reset()method.
- $f->
reset() -
Reset the internal line iterator used by
read_line().
- $f->write($content)
-
This is an atomic-write. First
$contentwill be written to a temporary file using'>'mode. Then the temporary file will be renamed to the desired file name. Under the hood this useswrite_file_atomic()from Test2::Harness::Util.
Source
The source code repository for Test2-Harness can be found at http://github.com/Test-More/Test2-Harness/.
Maintainers
- Chad Granum <[email protected]>
Releases
2 releases since Debian 12 (Bookworm); the text changed 1 time.