1No::Worries::File(3) User Contributed Perl Documentation No::Worries::File(3)
2
3
4
6 No::Worries::File - file handling without worries
7
9 use No::Worries::File qw(file_read file_write file_update);
10
11 # read a file
12 $data = file_read($path);
13
14 # idem but with data returned by reference
15 file_read($path, data => \$data);
16
17 # write a file
18 file_write($path, data => "hello world");
19
20 # idem but with data passed by reference
21 file_write($path, data => \"hello world");
22
23 # verbosely update a file
24 file_update($path, data => "hello world", diff => 1);
25
27 This module eases file handling by providing convenient wrappers around
28 standard file functions. All the functions die() on error.
29
31 This module provides the following functions (none of them being
32 exported by default):
33
34 file_read(PATH[, OPTIONS])
35 read the file at the given path and return its contents; supported
36 options:
37
38 • "binary": treat the file as binary
39
40 • "binmode": binary mode to use
41
42 • "bufsize": buffer size to use for I/O operations
43
44 • "data": return the file contents via this scalar reference or
45 code reference
46
47 • "handle": file handle to use
48
49 file_write(PATH[, OPTIONS])
50 write the given contents to the file at the given path; supported
51 options:
52
53 • "binary": treat the file as binary
54
55 • "binmode": binary mode to use
56
57 • "bufsize": buffer size to use for I/O operations
58
59 • "data": provide the file contents via this scalar, scalar
60 reference or code reference
61
62 • "handle": file handle to use
63
64 file_update(PATH[, OPTIONS])
65 check the text file at the given path and update it if needed,
66 printing what has been done on stdout; supported options:
67
68 • "data": provide the file contents via this scalar
69
70 • "diff": show differences
71
72 • "noaction": do not update the file
73
74 • "silent": do not print any message on stdout
75
77 Both file_read() and file_write() support a "handle" option that can
78 contain a file handle to use. When given, this handle will be used (and
79 closed at the end of the I/O operations) as is, without calling
80 binmode() on it (see below).
81
82 These functions also support a "binary" option and a "binmode" option
83 specifying how the file handle should be treated with respect to
84 binmode().
85
86 If "binmode" is set, binmode() will be used with the given layer.
87
88 If "binmode" is not set but "binary" is true, binmode() will be used
89 without any layer.
90
91 If neither "binmode" nor "binary" are set, binmode() will not be used.
92 This is the default.
93
94 file_read() can be given a code reference via the "data" option. Each
95 time data is read via sysread(), the subroutine will be called with the
96 read data. At the end of the file, the subroutine will be called with
97 an empty string.
98
99 file_write() can be given a code reference via the "data" option. It
100 should return data in a way similar to sysread(), returning an empty
101 string to indicate the end of the data to be written to the file.
102
103 file_update() only supports text files (no "binary" or "binmode"
104 options) and the "data" option can only be a scalar.
105
107 This module uses the following global variables (none of them being
108 exported):
109
110 $DefaultBufSize
111 default buffer size to use for I/O operations if not specified via
112 the "bufsize" option (default: 1MB)
113
115 No::Worries.
116
118 Lionel Cons <http://cern.ch/lionel.cons>
119
120 Copyright (C) CERN 2012-2019
121
122
123
124perl v5.36.0 2023-01-20 No::Worries::File(3)