Monday, March 24, 2014

Perl: Reading from file, read, eof

Perl: Reading from file, read, eof


  • read
  • eof
In Perl we usually care about lines of input so the above is enough. Still some like to read files with chunks of arbitrary length. read puts the read string to the variable passed to the function and returns the number of characters actually read READ_LENGTH = read FILEHANDLE,SCALAR,LENGTH

examples/files/read_file.pl
 
#!/usr/bin/perl
use strict;
use warnings;

# reading in 30 characters:

open my $in, "<", $0 or die $!;
my $expected = 30;
my $buf;
my $actual = read $in, $buf, $expected;
if ($actual < $expected) {
    print "reached end of file\n";
}

# returns TRUE if we are stand at or after the end of file.
eof($in)
 
--source: Edu Maven 

No comments: