while(my $buffer = <$client_socket>){ .... open(my $fh, '>>', $output) or die "Could not open file '$output' $!"; print $fh $buffer; }