Read File
2002-07-15 11:13:59
Category: perl:files
Description: Read in a file and print its contents using perl.
Author: detour
Viewed: 11373
Rating: (40 votes)


#!/usr/bin/perl
# readfile.pl by detour@metalshell.com
#
# Read in a file and print it out.
#
# http://www.metalshell.com/
 
use strict;
use Fcntl ':flock'; # contains LOCK_EX (2) and LOCK_UN (8) constants
 
my $buffer;
my $file;
 
$file = $ARGV[0];
 
open(INFILE, $file);
# request an exclusive lock on the file.
flock(INFILE, LOCK_EX);
# read in each line from the file
while (<INFILE>)
{
  # $_ is the line that <INFILE> has set.
  print "Line: ", $_;
}
# unlock the file.
flock(INFILE, LOCK_UN);
close(INFILE);