1Test2::Harness::Util::FUisleer(3C)ontributed Perl DocumeTnetsatt2i:o:nHarness::Util::File(3)
2
3
4
6 Test2::Harness::Util::File - Utility class for manipulating a file.
7
9 This is a utility class for file operations. This also serves as a base
10 class for several file helpers.
11
13 use Test2::Harness::Util::File;
14
15 my $f = Test2::Harness::Util::File->new(name => '/path/to/file');
16
17 $f->write($content);
18
19 my $fh = $f->open_file('<');
20
21 # Read, throw exception if it cannot read
22 my $content = $f->read();
23
24 # Try to read, but do not throw an exception if it cannot be read.
25 my $content_or_undef = $f->maybe_read();
26
27 my $line1 = $f->read_line();
28 my $line2 = $f->read_line();
29 ...
30
32 $filename = $f->name;
33 Get the filename. Must also be provided during construction.
34
35 $bool = $f->done;
36 True if read_line() has read every line.
37
39 $decoded = $f->decode($encoded)
40 This is a no-op, it returns the argument unchanged. This is called
41 by "read" and "read_line". Subclasses can override this if the file
42 contains encoded data.
43
44 $encoded = $f->encode($decoded)
45 This is a no-op, it returns the argument unchanged. This is called
46 by "write". Subclasses can override this if the file contains
47 encoded data.
48
49 $bool = $f->exists()
50 Check if the file exists
51
52 $content = $f->maybe_read()
53 This will read the file if it can and return the content (all lines
54 joined together as a single string). If the file cannot be read, or
55 does not exist this will return undef.
56
57 $fh = $f->open_file()
58 $fh = $f->open_file($mode)
59 Open a handle to the file. If no $mode is provided '<' is used.
60
61 $content = $f->read()
62 This will read the file if it can and return the content (all lines
63 joined together as a single string). If the file cannot be read, or
64 does not exist this will throw an exception.
65
66 $line = $f->read_line()
67 Read a single line from the file, subsequent calls will read the
68 next line and so on until the end of the file is reached. Reset
69 with the "reset()" method.
70
71 $f->reset()
72 Reset the internal line iterator used by "read_line()".
73
74 $f->write($content)
75 This is an atomic-write. First $content will be written to a
76 temporary file using '>' mode. Then the temporary file will be
77 renamed to the desired file name. Under the hood this uses
78 "write_file_atomic()" from Test2::Harness::Util.
79
81 The source code repository for Test2-Harness can be found at
82 http://github.com/Test-More/Test2-Harness/.
83
85 Chad Granum <exodist@cpan.org>
86
88 Chad Granum <exodist@cpan.org>
89
91 Copyright 2020 Chad Granum <exodist7@gmail.com>.
92
93 This program is free software; you can redistribute it and/or modify it
94 under the same terms as Perl itself.
95
96 See http://dev.perl.org/licenses/
97
98
99
100perl v5.34.0 2021-11-05 Test2::Harness::Util::File(3)