But there's a more elegant way to do it that doesn't rely on shells, and works more consistently. In fact TIMTOWTDI.
One way is fill the code with:
if ($outputInFile) { print $file "foo";}
else{print "foo"}
/me shivers ...
I found a way to do it quite elegantly redirecting a file handler where I'll be printing to STDOUT, using Typeglobs. I'm not too confident managing typeglobs, but it seems to work :)
I asked at #barcelona.pm and alexm (O HAI! president) told me I could think it the other way around, and overwrite STDOUT to an opened filehandle in case I needed the redirection. I didn't know I could handle STDOUT like any other fh. It's nice to know it.
I think Casiano taught me (back in university times) another way to do it (maybe it was using tee, or some IO::Handle funky stuff).
Is there any cpan module that does that kind of Stdout vs file output? I haven't found it, but it MUST be there. Or maybe the code to do it is so small it doesn't make sense writing a module for that