#!/usr/bin/perl use strict; use warnings; use Getopt::Std; my $opt = {}; Get_Args( $opt ); my $fh; if ( $opt->{o} ) { open ( $fh, '>', $opt->{o} ) or die "Unable to open $opt->{o} for writing : $!"; } else { open ( $fh, '>&', *STDOUT ) or die "Unable to dup STDOUT : $!"; } select $fh; $| = 1; print "foo bar\n"; sub Get_Args { my $opt = shift; my $Usage = qq{Usage: $0 [options] -h : This help message. -o : Output file - STDOUT by default } . "\n"; getopts( 'ho:' , $opt ) or die $Usage; die $Usage if $opt->{h}; }