KumarRana
KumarRana

Reputation: 103

read two text files as an argument and display it's contents using perl

I have two text files and I want to read them by passing argument at command line.

Now how to take second file? When I give the second file name command line is not reading. Please suggest.

I have used $ARGV[0] and $ARGV[1] in the code to pass the arguments at command line.

Upvotes: 0

Views: 202

Answers (2)

serenesat
serenesat

Reputation: 4709

my ($file1, $file2) = @ARGV;
open my $fh1, '<', $file1 or die $!;
open my $fh2, '<', $file2 or die $!;
while (<$fh1>) {
    do something with $_
}
while (<$fh2>) {
    do something with $_
}
close $fh1;
close $fh2;

Where $_ is the default variable.

run as:

perl readingfile.pl filename1 filename2

Upvotes: 1

Oleksandr Mazur
Oleksandr Mazur

Reputation: 21

$ ./read.pl file1 file2

Reading file1 Reading file2

$ cat read.pl 
#!/usr/bin/perl
use strict;
use warnings;

readFile($_) for @ARGV;

sub readFile {
my $filename = shift;
print "Reading $filename\n";

#OPEN CLOSE stuff here

}

Upvotes: 2

Related Questions