Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Mention .lines when reading a text file
  • Loading branch information
Paul Cochrane committed Feb 28, 2015
1 parent 88af0dc commit bb3a7f5
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/Language/5to6.pod
Expand Up @@ -971,7 +971,35 @@ Removed. See S19.
This is now the default behavior.
=head2 File-related operations
=head3 Reading the lines of a text file into an array
In Perl 5, a common idiom for reading the lines of a text file goes
something like this:
open my $fh, "<", "file" or die "$!";
my @lines = <$fh>;
close $fh;
In Perl 6, this has been simplified to
my @lines = "file".IO.lines;
Do I<not> be tempted to try slurping in a file and splitting the resulting
string on newlines as this will give an array with a trailing empty element,
which is one more than you probably expect (it's also more complicated),
e.g.:
# initialize the file to read
spurt "test-file", q:to/END/;
first line
second line
third line
END
# read the file
my @lines = "test-file".IO.slurp.split(/\n/);
say @lines.elems; #-> 4
=head2 Misc.
Expand All @@ -985,8 +1013,6 @@ compiled bytecode.
Rakudo supports this only for modules so far.
=head2 Core modules
=head4 C<Data::Dumper>
Expand Down

0 comments on commit bb3a7f5

Please sign in to comment.