my $file = "file.txt"; print "before\n"; { local *STDOUT; open (STDOUT, ">$file") or die; system("ls"); } print "after\n";