Skip to content

Commit 97a01da

Browse files
committed
make examples compile
1 parent e1fd532 commit 97a01da

File tree

1 file changed

+63
-50
lines changed

1 file changed

+63
-50
lines changed

doc/Language/io.pod6

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ L<IO::Path|/type/IO::Path> types.
1414
One way to read the contents of a file is to open the file via the C<open>
1515
function with the C<:r> (read) file mode option and slurp in the contents:
1616
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;
2021
2122
Here we explicitly close the file handle using the C<close> method on the
2223
C<IO::Handle> object. This is a very traditional way of reading the
2324
contents of a file. However, the same can be done more easily and clearly
2425
like so:
2526
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"
2931
3032
By adding the C<IO> role to the file name string, we are effectively able to
3133
refer to the string as the file object itself and thus slurp in its
@@ -37,55 +39,63 @@ the file for you.
3739
Of course, we also have the option to read a file line-by-line. The new line
3840
separator (i.e., C<$*IN.nl-in>) will be excluded.
3941
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+
}
4346
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;
4649
4750
=head1 Writing to files
4851
4952
To write data to a file, again we have the choice of the traditional method
5053
of calling the C<open> function -- this time with the C<:w> (write) option
5154
-- and printing the data to the file:
5255
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;
5660
5761
Or equivalently with C<say>, thus the explicit newline is no longer necessary:
5862
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;
6267
6368
We can simplify this by using C<spurt> to open the file in write mode,
6469
writing the data to the file and closing it again for us:
6570
66-
spurt "testfile", "data and stuff\n";
71+
=for code :skip-test
72+
spurt "testfile", "data and stuff\n";
6773
6874
By default all (text) files are written as UTF-8, however if necessary, an
6975
explicit encoding can be specified via the C<:enc> option:
7076
71-
spurt "testfile", "latin1 text: äöüß", enc => "latin1";
77+
=for code :skip-test
78+
spurt "testfile", "latin1 text: äöüß", enc => "latin1";
7279
7380
To append to a file, specify the C<:a> option when opening the file handle
7481
explicitly,
7582
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;
7987
8088
or equivalently with C<say>, thus the explicit newline is no longer necessary,
8189
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;
8594
8695
or even simpler with the C<:append> option in the call to C<spurt>:
8796
88-
spurt "testfile", "more data\n", :append;
97+
=for code :skip-test
98+
spurt "testfile", "more data\n", :append;
8999
90100
To explicitly write binary data to a file, open it with the C<:bin> option.
91101
The input/output operations then will take place using the C<Buf> type instead
@@ -96,43 +106,48 @@ of the C<Str> type.
96106
Use the C<e> method on an C<IO::Handle> object to test whether the file or
97107
directory exists.
98108
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+
}
105116
106117
It is also possible to use the colon pair syntax to achieve the same thing:
107118
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+
}
111123
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+
}
116128
117129
Similarly to the file existence check, one can also check to see if a path
118130
is a directory. For instance, assuming that the file C<testfile> and the
119131
directory C<lib> exist, we would obtain from the existence test method C<e>
120132
the same result, namely that both exist:
121133
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
124137
125138
However, since only one of them is a directory, the directory test method
126139
C<d> will give a different result:
127140
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
130144
131145
Naturally the tables are turned if we check to see if the path is a file via
132146
the file test method C<f>:
133147
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
136151
137152
=head1 Getting a directory listing
138153
@@ -151,22 +166,20 @@ as an argument to C<dir>:
151166
To create a new directory, simply call the C<mkdir> function with the
152167
directory name as its argument:
153168
169+
=for code :skip-test
154170
mkdir "newdir";
155171
156172
The function returns the name of the created directory on success and C<Nil>
157173
on failure. Thus the standard Perl idiom works as expected:
158174
175+
=for code :skip-test
159176
mkdir "newdir" or die "$!";
160177
161178
Use C<rmdir> to remove I<empty> directories:
162179
180+
=for code :skip-test
163181
rmdir "newdir" or die "$!";
164182
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-
170183
=end pod
171184

172185
# vim: expandtab shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)