in reply to can you assign file handles?

I think that the following would achieve what you require:
if ("true" eq $flag) { open (OUT, ">output.txt") or die ("could not open output.txt: $!") +; } else { open (OUT, ">&STDOUT"); }

Update:Changed as per bbfu's comment below, although I kept the duplication because IMHO it makes it easier for a newbie to follow.

*~-}hotyopa{-~*

Replies are listed 'Best First'.
Re: Re: can you assign file handles?
by bbfu (Curate) on Jan 30, 2001 at 07:23 UTC

    Whoa... Actually, you would need

    if ("true" eq $flag) {

    or

    if ($flag) {

    as the condtion of the if statement (I'm still not sure if they wanted to actually test for boolean truth or equality to the string "true"). Assignment's right out.

    But the reopening of OUT is definately what they wanted! =) (Though I think I, personally, actually prefer Fastolfe's method of using the trinary operator and one open statement so as not to duplicate code and so you test the dup-open for success as well. TIMTOWTDI. =)

    bbfu