How would one be able to do this? I know you have to use <>.I already tried giving it a shot,but it might just because I am on Windows.
Asked
Active
Viewed 3,594 times
-1
-
Why wouldn't you just use the `open` function to open the file *then* read the files? – Hunter McMillen Jul 05 '13 at 20:59
-
My program has to be able to get the file names from command line. Example: ./program.pl file1 file2 – Michelle Moya Jul 05 '13 at 21:01
2 Answers
4
# run as:
# perl my_script1.pl file1 file2
my ($file1, $file2) = @ARGV;
open my $fh1, '<', $file1;
open my $fh2, '<', $file2;
while (<$fh1>) {
... do something with $_ from $file1 ...
}
while (<$fh2>) {
... do something with $_ from $file2 ...
}
close $fh1;
close $fh2;

mob
- 117,087
- 18
- 149
- 283
-
Doesn't work on Windows,I am pretty sure it will work on Linux though,thanks! – Michelle Moya Jul 05 '13 at 21:07
-
I'm pretty sure it will work on Windows, too. Can you be more specific about how it doesn't work? – mob Jul 05 '13 at 21:11
-
0
You didn't state whether you need to treat the files differently or not. If not, then you can just use <>
without needing to call open
explicitly.
while (<>) {
# Process $_
}
will work fine. When it gets to the end of the first file, <>
will continue on with reading the second file. And so on until it runs out of files to read.

Dave Sherohman
- 45,363
- 14
- 64
- 102