Example 3-4. examples/files/read_file.p6
#!/usr/bin/perl6
my $filename = "examples/files/read_file.p6";
if (my $fh = open $filename, :r) {
for $fh.readline -> $line {
say $line;
}
} else {
say "Could not open '$filename'";
}
The for loop in our example repeatedly calls =$fh and
on ech iteration it places the retreived string in the $line variable.
This script is very similar to what the unix cat command does.
| Prev | Home (Copyright Gabor Szabo) Perl Training Israel | Next |
| Read line from file | Up | Write to a file |