in reply to can you assign file handles?

Note that "-" is a valid filename for use with open, and is equivalent to opening STDOUT (or STDIN, depending upon how you use it). You might work that into your logic.
open(OUT, $flag ? ">>output.txt" : ">>-") or die "...";