Reputation: 69
I am very new to Perl (just seen one Youtube video on it). I want to make a script that takes will take two .csv files and append them together and make a new .csv file. I don't want the two .csv files that are being appended to be altered. I also want to make this script take in a user input as the file to be merged (right now the two appended .csv files are in the same directory).
The error I keep getting is: print() on closed filehandle OUT at line 1 (#2) (W closed) The filehande you're printing on got itself closed sometime before now. Check your control flow
But I never used the close command so how are my filehandles closing?
use strict;
use warnings;
use diagnostics;
use feature 'say';
use feature "switch";
use v5.22;
# Ask for Base File
say "What is the base file you want to use?";
my $base_file = <STDIN>;
chomp $base_file;
open (BASE, '<', $base_file) or die "Couldn't find the base file you are entered: $base_file ";
# Ask for Append File
say "What is the file you want to append to the base file?";
my $append_file = <STDIN>;
chomp $append_file;
open (APPEND, '<', $append_file) or die "Couldn't find the append file you are entered: $append_file ";
# Create new File with new name
say "What is the name of the new file you want to create?";
my $new_file = <STDIN>;
open (OUT, '>>', $new_file);
chomp $new_file;
while(my $base_line = <BASE>) {
chomp $base_line;
print OUT $base_line;
}
Upvotes: 3
Views: 1436
Reputation: 386706
You really should check if calls to open
succeed.
open(OUT, '>>', $new_file)
or die("Can't append to \"$new_file\": $!\n");
I bet you'll find that the open
is failing, I bet you'll find that it's because the file you specified doesn't exist, and I bet you'll find that $new_file
contains a line feed it shouldn't.
The fix is to move the following line to before the open
:
chomp $new_file;
By the way, you shouldn't be using global variables. Replace OUT
with my $OUT
. Same for BASE
and APPEND
.
Upvotes: 4