@@ -132,21 +132,37 @@ C<$enc> argument switches the encoding used by the handle.
132
132
}
133
133
= end code
134
134
135
- = head2 method lines
135
+ = head2 routines lines
136
136
137
137
Defined as:
138
138
139
- method lines($limit = Inf)
139
+ sub lines(IO::Handle:D $fh = $*ARGFILES, $limit = Inf, :$close --> Seq:D)
140
+ method lines(IO::Handle:D: $limit = Inf, :$close --> Seq:D)
140
141
141
- Return a lazy list of the file's lines read via L < get > , limited to C < $limit > lines.
142
- The new line separator (i.e., C < $*IN.nl-in > ) will be excluded.
142
+ Return a L < Seq > each element of which is a line from the handle (that is
143
+ chunks deliniated by L « C < .nl-in > |/type/IO::Handle#method_nl-in» ). If the
144
+ handle's L « C < .chomp > |/type/IO::Handle#method_new» attribute is set to C < True > ,
145
+ then characters specified by L « C < .nl-in > |/type/IO::Handle#method_nl-in» will be
146
+ stripped from each line.
143
147
144
- = for code :skip-test
145
- my @data;
146
- my $data-file = open 'readings.csv';
147
- for $data-file.lines -> $line {
148
- @data.push($line.split(','))
149
- }
148
+ Reads up to C < $limit > lines, where C < $limit > can be a non-negative L < Int > ,
149
+ C < Inf > , or L < Whatever > (which is interpreted to mean C < Inf > ). If C < :$close > is
150
+ set to C < True > , will close the handle when the file ends or C < $limit > is
151
+ reached. Subroutine form defaults to
152
+ L « C < $*ARGFILES > |/language/variables#index-entry-%24%2AARGFILES» , if no
153
+ handle is provided.
154
+
155
+ B < NOTE: > the lines are read lazily, so ensure the returned L < Seq > is either
156
+ L < fully reified|/language/glossary#index-entry-Reify > or is no longer needed
157
+ when you close the handle or attempt to use any other methods that change the
158
+ file position.
159
+
160
+ = begin code :skip-test
161
+ say "The file contains ",
162
+ '50GB-file'.IO.open.lines.grep(*.contains: 'Perl').elems,
163
+ " lines that mention Perl";
164
+ # OUTPUT: «The file contains 72 lines that mention Perl»
165
+ = end code
150
166
151
167
= head2 method lock
152
168
0 commit comments