@@ -16,7 +16,7 @@ To read in the contents of a file one could open the file via the C<open>
16
16
function with the C < :r > (read) file mode option and slurp in the contents:
17
17
18
18
my $fh = open "testfile", :r;
19
- my $contents = $fh.slurp;
19
+ my $contents = $fh.slurp-rest ;
20
20
$fh.close;
21
21
22
22
where we explicitly close the file handle using the C < close > method on the
@@ -25,6 +25,8 @@ contents of a file. However, we can do the same much more easily and
25
25
clearly like so:
26
26
27
27
my $contents = "testfile".IO.slurp;
28
+ # or in procedural form:
29
+ my $contents = slurp "testfile"
28
30
29
31
By adding the C < IO > role to the file name string, we are effectively able to
30
32
refer to the string as the file object itself and thus slurp in its
@@ -73,21 +75,21 @@ exist. So, how do we know that the file even exists before reading it?
73
75
This is what the C < e > method on an C < IO::Handle > object is for.
74
76
75
77
if "nonexistent_file".IO.e {
76
- say "file exists";
78
+ say "file exists";
77
79
}
78
80
else {
79
- say "file doesn't exist";
81
+ say "file doesn't exist";
80
82
}
81
83
82
84
It is also possible to use the colon pair syntax to achieve the same thing:
83
85
84
86
if "path/to/file".IO ~~ :e {
85
- say 'file exists';
87
+ say 'file exists';
86
88
}
87
89
88
90
my $file = "path/to/file";
89
91
if $file.IO ~~ :e {
90
- say 'file exists';
92
+ say 'file exists';
91
93
}
92
94
93
95
Similarly to the file existence check, one can also check to see if a path
@@ -113,7 +115,7 @@ the file test method C<f>:
113
115
= head1 Getting a directory listing
114
116
115
117
To list the files and directories in the current directory, one merely needs
116
- to use the C < dir > function. This returns a list of C < IO::Path > objects one
118
+ to use the C < dir > function. This returns a list of L < IO::Path > objects one
117
119
can then write to, read from etc.
118
120
119
121
say dir; # "/path/to/testfile".IO "/path/to/lib".IO
@@ -147,3 +149,5 @@ TODO: http://doc.perl6.org/type/IO::Handle has close but no open
147
149
= end comment
148
150
149
151
= end pod
152
+
153
+ # vim: expandtab
0 commit comments