@@ -14,18 +14,20 @@ L<IO::Path|/type/IO::Path> types.
14
14
One way to read the contents of a file is to open the file via the C < open >
15
15
function with the C < :r > (read) file mode option and slurp in the contents:
16
16
17
- my $fh = open "testfile", :r;
18
- my $contents = $fh.slurp-rest;
19
- $fh.close;
17
+ = for code :skip-test
18
+ my $fh = open "testfile", :r;
19
+ my $contents = $fh.slurp-rest;
20
+ $fh.close;
20
21
21
22
Here we explicitly close the file handle using the C < close > method on the
22
23
C < IO::Handle > object. This is a very traditional way of reading the
23
24
contents of a file. However, the same can be done more easily and clearly
24
25
like so:
25
26
26
- my $contents = "testfile".IO.slurp;
27
- # or in procedural form:
28
- my $contents = slurp "testfile"
27
+ = for code :skip-test
28
+ my $contents = "testfile".IO.slurp;
29
+ # or in procedural form:
30
+ $contents = slurp "testfile"
29
31
30
32
By adding the C < IO > role to the file name string, we are effectively able to
31
33
refer to the string as the file object itself and thus slurp in its
@@ -37,55 +39,63 @@ the file for you.
37
39
Of course, we also have the option to read a file line-by-line. The new line
38
40
separator (i.e., C < $*IN.nl-in > ) will be excluded.
39
41
40
- for 'huge-csv'.IO.lines -> $line {
41
- # Do something with $line
42
- }
42
+ = for code :skip-test
43
+ for 'huge-csv'.IO.lines -> $line {
44
+ # Do something with $line
45
+ }
43
46
44
- # or if you'll be processing later
45
- my @lines = 'huge-csv'.IO.lines;
47
+ # or if you'll be processing later
48
+ my @lines = 'huge-csv'.IO.lines;
46
49
47
50
= head1 Writing to files
48
51
49
52
To write data to a file, again we have the choice of the traditional method
50
53
of calling the C < open > function -- this time with the C < :w > (write) option
51
54
-- and printing the data to the file:
52
55
53
- my $fh = open "testfile", :w;
54
- $fh.print("data and stuff\n");
55
- $fh.close;
56
+ = for code :skip-test
57
+ my $fh = open "testfile", :w;
58
+ $fh.print("data and stuff\n");
59
+ $fh.close;
56
60
57
61
Or equivalently with C < say > , thus the explicit newline is no longer necessary:
58
62
59
- my $fh = open "testfile", :w;
60
- $fh.say("data and stuff");
61
- $fh.close;
63
+ = for code :skip-test
64
+ my $fh = open "testfile", :w;
65
+ $fh.say("data and stuff");
66
+ $fh.close;
62
67
63
68
We can simplify this by using C < spurt > to open the file in write mode,
64
69
writing the data to the file and closing it again for us:
65
70
66
- spurt "testfile", "data and stuff\n";
71
+ = for code :skip-test
72
+ spurt "testfile", "data and stuff\n";
67
73
68
74
By default all (text) files are written as UTF-8, however if necessary, an
69
75
explicit encoding can be specified via the C < :enc > option:
70
76
71
- spurt "testfile", "latin1 text: äöüß", enc => "latin1";
77
+ = for code :skip-test
78
+ spurt "testfile", "latin1 text: äöüß", enc => "latin1";
72
79
73
80
To append to a file, specify the C < :a > option when opening the file handle
74
81
explicitly,
75
82
76
- my $fh = open "testfile", :a;
77
- $fh.print("more data\n");
78
- $fh.close;
83
+ = for code :skip-test
84
+ my $fh = open "testfile", :a;
85
+ $fh.print("more data\n");
86
+ $fh.close;
79
87
80
88
or equivalently with C < say > , thus the explicit newline is no longer necessary,
81
89
82
- my $fh = open "testfile", :a;
83
- $fh.say("more data");
84
- $fh.close;
90
+ = for code :skip-test
91
+ my $fh = open "testfile", :a;
92
+ $fh.say("more data");
93
+ $fh.close;
85
94
86
95
or even simpler with the C < :append > option in the call to C < spurt > :
87
96
88
- spurt "testfile", "more data\n", :append;
97
+ = for code :skip-test
98
+ spurt "testfile", "more data\n", :append;
89
99
90
100
To explicitly write binary data to a file, open it with the C < :bin > option.
91
101
The input/output operations then will take place using the C < Buf > type instead
@@ -96,43 +106,48 @@ of the C<Str> type.
96
106
Use the C < e > method on an C < IO::Handle > object to test whether the file or
97
107
directory exists.
98
108
99
- if "nonexistent_file".IO.e {
100
- say "file exists";
101
- }
102
- else {
103
- say "file doesn't exist";
104
- }
109
+ = for code :skip-test
110
+ if "nonexistent_file".IO.e {
111
+ say "file exists";
112
+ }
113
+ else {
114
+ say "file doesn't exist";
115
+ }
105
116
106
117
It is also possible to use the colon pair syntax to achieve the same thing:
107
118
108
- if "path/to/file".IO ~~ :e {
109
- say 'file exists';
110
- }
119
+ = for code :skip-test
120
+ if "path/to/file".IO ~~ :e {
121
+ say 'file exists';
122
+ }
111
123
112
- my $file = "path/to/file";
113
- if $file.IO ~~ :e {
114
- say 'file exists';
115
- }
124
+ my $file = "path/to/file";
125
+ if $file.IO ~~ :e {
126
+ say 'file exists';
127
+ }
116
128
117
129
Similarly to the file existence check, one can also check to see if a path
118
130
is a directory. For instance, assuming that the file C < testfile > and the
119
131
directory C < lib > exist, we would obtain from the existence test method C < e >
120
132
the same result, namely that both exist:
121
133
122
- say "testfile".IO.e; # True
123
- say "lib".IO.e; # True
134
+ = for code :skip-test
135
+ say "testfile".IO.e; # True
136
+ say "lib".IO.e; # True
124
137
125
138
However, since only one of them is a directory, the directory test method
126
139
C < d > will give a different result:
127
140
128
- say "testfile".IO.d; # False
129
- say "lib".IO.d; # True
141
+ = for code :skip-test
142
+ say "testfile".IO.d; # False
143
+ say "lib".IO.d; # True
130
144
131
145
Naturally the tables are turned if we check to see if the path is a file via
132
146
the file test method C < f > :
133
147
134
- say "testfile".IO.f; # True
135
- say "lib".IO.f; # False
148
+ = for code :skip-test
149
+ say "testfile".IO.f; # True
150
+ say "lib".IO.f; # False
136
151
137
152
= head1 Getting a directory listing
138
153
@@ -151,22 +166,20 @@ as an argument to C<dir>:
151
166
To create a new directory, simply call the C < mkdir > function with the
152
167
directory name as its argument:
153
168
169
+ = for code :skip-test
154
170
mkdir "newdir";
155
171
156
172
The function returns the name of the created directory on success and C < Nil >
157
173
on failure. Thus the standard Perl idiom works as expected:
158
174
175
+ = for code :skip-test
159
176
mkdir "newdir" or die "$!";
160
177
161
178
Use C < rmdir > to remove I < empty > directories:
162
179
180
+ = for code :skip-test
163
181
rmdir "newdir" or die "$!";
164
182
165
- = begin comment
166
- TODO: base on https://github.com/perl6/specs/blob/master/S32-setting-library/IO.pod?
167
- TODO: https://docs.perl6.org/type/IO::Handle has close but no open
168
- = end comment
169
-
170
183
= end pod
171
184
172
185
# vim: expandtab shiftwidth=4 ft=perl6
0 commit comments