You are here:

Perl & CGI/File handles in perl script


Hi Lukas,

I need to fetch filenames say filename1,filename2,.. from a file and then fetch data from those files.So I need to read two file everytime. Can I go with declaring two file handles or else if there is some other way pls guide me with example.
Awaiting reply.


If I understand your question correctly - you want to know if you can read in two files using the same file handle. You can do this but the files need to read one at a time.

Here is a snippet of reading in your two files:

my $fh;
my @data1;
my @data2;

# Reads in first file handle.
open $fh, '<', 'file1' || die "error reading file $!\n";

@data1 = <$fh>;

close $fh;

undef $fh;

# Reads in the second file handle.
open $fh, '<', 'file2' || die "error reading file $!\n";

@data2 = <$fh>;

close $fh;

# ... process your data

If this is not what you are trying to do, let me know,



Perl & CGI

All Answers

Answers by Expert:

Ask Experts


Lukas Neuburger


I can answer a wide range of Perl questions, everything from basic language syntax to popular CPAN modules, to web/CGI scripting and much, much more.


As an experienced technical professional, I leverage the power and flexibility of Perl to develop solutions in a variety of areas, including: web development, database management, and systems administration to name only a few.

Bachelors of Science in Computer Science

©2016 All rights reserved.