logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Test2::Harness::Util::File - Utility class for manipulating a file.

Attributes

       $filename = $f->name;
           Get the filename. Must also be provided during construction.

       $bool = $f->done;
           True if read_line() has read every line.

Authors

       Chad Granum <exodist@cpan.org>

Description

       This is a utility class for file operations. This also serves as a base class for several file helpers.

Maintainers

       Chad Granum <exodist@cpan.org>

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 $mode is 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 $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.

Name

       Test2::Harness::Util::File - Utility class for manipulating a file.

Source

       The source code repository for Test2-Harness can be found at http://github.com/Test-More/Test2-Harness/.

Synopsis

           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();
           ...

See Also