use strict; use warnings; my $outfile = "test.txt"; open (my $ofh, ">$outfile") or die; foo ($ofh); #Prints to the file pointed by $ofh foo(); #Prints to STDOUT sub foo { my $filehandle= shift; $filehandle ||= \*STDOUT ; select($filehandle); print <<"EOF"; This is a test, it is only a test. In the event of an emergency, run around in a panic. EOF }