Perl5 -> Perl6: a file into an array
One of the most common task I find in my Perl programs is to read data from text files, and more often that what I would expect, I need to slurp the file (read all the lines at once). Well, to be more precise, I need to read all content into an array (while slurping is to read all the content into a scalar). In Perl 5 I tend to use the following snippet:
my @headers;
if ( @ARGV > 1 ){
open my $tag, "<", $ARGV[ 1 ] || die "\nError\n";
local $/ = "\r\n";
chomp( @headers = <$tag> );
unshift @headers, '#NUM' if ( @headers );
close $tag;
}
- define the
@headers
empty list; - check if the name of the file to read has been passed as argument;
- open such file;
- change the input field separator (DOS mode), not always required;
- read all lines into
@header
and clean each line; - place other piece of data into the array;
- close the file handle.
my @headers;
if ( $file_tag.defined ) {
@headers = $file_tag.IO.lines( :chomp );
@headers.unshift( '#NUM' ) if ( @headers );
}
MAIN
function I do not have to check for the arity of arguments, but can
check on the argument itself (in this case $file_tag
).
The lines
method allows to get all the content at once, or well, lazyly, but since assigning a Seq
to an array
reifies it, it works as reading all the file and putting the lines into the array without any risk of leaving a pending file handle or missing
any line. It is interesting to note that the lines
method allows a boolean attribute, chomp
to perform the clean up of each line.
Since the method delegates to IO::Handle.lines
, and since by default the latter uses both "\n"
and "\r\n"
to clean up lines, all the magic is done for me.
Most notably, the chomp
attribute is true by default, so I would have not declared it at all!
The remaining is quite obvious.`