Skip to content

Commit 49e58bd

Browse files
committed
[io grant] Document IO::Handle.lines
- Include &lines - Clarify behaviour vis-a-vis laziness - Document :$close - Remove bad practices from example
1 parent 5742222 commit 49e58bd

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

doc/Type/IO/Handle.pod6

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,37 @@ C<$enc> argument switches the encoding used by the handle.
132132
}
133133
=end code
134134
135-
=head2 method lines
135+
=head2 routines lines
136136
137137
Defined as:
138138
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)
140141
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.
143147
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
150166
151167
=head2 method lock
152168

0 commit comments

Comments
 (0)