my $output_filename = './test.cpp'; open $OUTPUT, '>', $output_filename or die "Can't open $output_filename for writing: $!"; foreach ( @data ) { print { $OUTPUT } "$_\n"; # Print to file } close $OUTPUT;