Test2::Harness::Util::File - Utility class for manipulating a file.
This is a utility class for file operations. This also serves as a base class for several file helpers.
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(); ...
Get the filename. Must also be provided during construction.
True if read_line() has read every line.
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.
read
read_line
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.
write
Check if the file exists
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.
Open a handle to the file. If no $mode is provided '<' is used.
'<'
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.
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.
reset()
Reset the internal line iterator used by read_line().
read_line()
This is an atomic-write. First $content will be written to a temporary file using '>' mode. Then the temporary file will be renamed to the desired file name. Under the hood this uses write_file_atomic() from Test2::Harness::Util.
'>'
write_file_atomic()
The source code repository for Test2-Harness can be found at http://github.com/Test-More/Test2-Harness/.
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/
To install Test2::Harness, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test2::Harness
CPAN shell
perl -MCPAN -e shell install Test2::Harness
For more information on module installation, please visit the detailed CPAN module installation guide.