use strict; use warnings; my $outfilename = "outfilename.txt"; # Use a variable for the outfile handle my $ofh; open($ofh, ">$outfilename") or die qq(Couldn't open file "$outfilename" for writing\n); ... insert more code here close $ofh;