.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Test2::Harness::Util::File 3pm" .TH Test2::Harness::Util::File 3pm "2023-03-12" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test2::Harness::Util::File \- Utility class for manipulating a file. .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is a utility class for file operations. This also serves as a base class for several file helpers. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Test2::Harness::Util::File; \& \& my $f = Test2::Harness::Util::File\->new(name => \*(Aq/path/to/file\*(Aq); \& \& $f\->write($content); \& \& my $fh = $f\->open_file(\*(Aq<\*(Aq); \& \& # 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(); \& ... .Ve .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" .ie n .IP "$filename = $f\->name;" 4 .el .IP "\f(CW$filename\fR = \f(CW$f\fR\->name;" 4 .IX Item "$filename = $f->name;" Get the filename. Must also be provided during construction. .ie n .IP "$bool = $f\->done;" 4 .el .IP "\f(CW$bool\fR = \f(CW$f\fR\->done;" 4 .IX Item "$bool = $f->done;" True if \fBread_line()\fR has read every line. .SH "METHODS" .IX Header "METHODS" .ie n .IP "$decoded = $f\->decode($encoded)" 4 .el .IP "\f(CW$decoded\fR = \f(CW$f\fR\->decode($encoded)" 4 .IX Item "$decoded = $f->decode($encoded)" This is a no-op, it returns the argument unchanged. This is called by \f(CW\*(C`read\*(C'\fR and \f(CW\*(C`read_line\*(C'\fR. Subclasses can override this if the file contains encoded data. .ie n .IP "$encoded = $f\->encode($decoded)" 4 .el .IP "\f(CW$encoded\fR = \f(CW$f\fR\->encode($decoded)" 4 .IX Item "$encoded = $f->encode($decoded)" This is a no-op, it returns the argument unchanged. This is called by \f(CW\*(C`write\*(C'\fR. Subclasses can override this if the file contains encoded data. .ie n .IP "$bool = $f\->\fBexists()\fR" 4 .el .IP "\f(CW$bool\fR = \f(CW$f\fR\->\fBexists()\fR" 4 .IX Item "$bool = $f->exists()" Check if the file exists .ie n .IP "$content = $f\->\fBmaybe_read()\fR" 4 .el .IP "\f(CW$content\fR = \f(CW$f\fR\->\fBmaybe_read()\fR" 4 .IX Item "$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. .ie n .IP "$fh = $f\->\fBopen_file()\fR" 4 .el .IP "\f(CW$fh\fR = \f(CW$f\fR\->\fBopen_file()\fR" 4 .IX Item "$fh = $f->open_file()" .PD 0 .ie n .IP "$fh = $f\->open_file($mode)" 4 .el .IP "\f(CW$fh\fR = \f(CW$f\fR\->open_file($mode)" 4 .IX Item "$fh = $f->open_file($mode)" .PD Open a handle to the file. If no \f(CW$mode\fR is provided \f(CW\*(Aq<\*(Aq\fR is used. .ie n .IP "$content = $f\->\fBread()\fR" 4 .el .IP "\f(CW$content\fR = \f(CW$f\fR\->\fBread()\fR" 4 .IX Item "$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. .ie n .IP "$line = $f\->\fBread_line()\fR" 4 .el .IP "\f(CW$line\fR = \f(CW$f\fR\->\fBread_line()\fR" 4 .IX Item "$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 \f(CW\*(C`reset()\*(C'\fR method. .ie n .IP "$f\->\fBreset()\fR" 4 .el .IP "\f(CW$f\fR\->\fBreset()\fR" 4 .IX Item "$f->reset()" Reset the internal line iterator used by \f(CW\*(C`read_line()\*(C'\fR. .ie n .IP "$f\->write($content)" 4 .el .IP "\f(CW$f\fR\->write($content)" 4 .IX Item "$f->write($content)" This is an atomic-write. First \f(CW$content\fR will be written to a temporary file using \f(CW\*(Aq>\*(Aq\fR mode. Then the temporary file will be renamed to the desired file name. Under the hood this uses \f(CW\*(C`write_file_atomic()\*(C'\fR from Test2::Harness::Util. .SH "SOURCE" .IX Header "SOURCE" The source code repository for Test2\-Harness can be found at \&\fIhttp://github.com/Test\-More/Test2\-Harness/\fR. .SH "MAINTAINERS" .IX Header "MAINTAINERS" .IP "Chad Granum " 4 .IX Item "Chad Granum " .SH "AUTHORS" .IX Header "AUTHORS" .PD 0 .IP "Chad Granum " 4 .IX Item "Chad Granum " .PD .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2020 Chad Granum . .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP See \fIhttp://dev.perl.org/licenses/\fR