Read line from file


As in other high level languages one has to open a file in order to read from it or to write to it. In Perl 6 it is done by the open() function imported from the IO class. It can receive several parameter but thw two are very important: The name of the file and the mode. In order to open a file for reading the mode need to be :r. The function either returns a file handle that should be placed in a scalar variable or returns undef in case of failure. $fh = open $filename, :r Once we have an open file handler we can use the get method ($fh.get) to read one line from the given file. One could read many lines using consecutive calls to the get method but there are better ways to do that. Currently I think Rakudo throws an exception if the file cannot be opened but I think the spec says that it should just return undef. The specifications of all the IO of Perl 6 can be found in S32-setting-library/IO.pod

examples/files/read_one_line.p6
#!/usr/bin/perl6
use v6;

my $filename = "examples/files/read_one_line.p6";

my $fh = open $filename;
my $line = $fh.get;
say $line;

Copyright 2006, 2007, 2008, 2009, 2010 Gabor Szabo http://szabgab.com/ Index | TOC
If you are interested in on-site trainings by the author, please contact me directly.

Online courses: