foreach my $lineData (@inputData) { @lineValue = split(',', $lineData); print $lineValue[0]; print $lineValue[1]; $output = $lineValue[0].".cfg"; $conf = "define hostgroup{\n\thostgroup_name\t\t" . $lineValue[0] . "\n\talias\t\t" . $lineValue[1] . "\n}\n"; open(my $fh2, ">", $output); print $fh2 $conf; close $fh2 or warn $! ? "Error closing sort pipe: $!" : "Exit status $? from sort"; }