Neither suggestion seems well-thought-out. For example, 1 under the second doesn't do what you think it does -- if $foo is not true, then it dies -- it ignores if the open fails or not. As to the first one, what is main?
Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).