Saturday 5 April 2014

How to read a file from script using PERL

#!/usr/bin/perl
use strict;
use warnings;

use Path::Class;
use autodie; # die if error in reading file

my $dir = dir("/tmp"); # /tmp

my $file = $dir->file("sample_file.txt");

# Read in the whole contents of the file 
 
my $stuff = $file->slurp();

# openr() returns an IO::File object to read from 
 
my $file_handle = $file->openr();

# Read one line at a time
 
 while( my $line = $file_handle->getline() ) {
        print $line;
}

No comments: