Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The 3 argument file open is "open HANDLE DIRECTION FILENAME". I wanted to have a "generic" open that supports both STDOUT as well as user-defined file names. I could not figure out a way to assign the pre-defined file handles STDOUT (& STDIN) to a filename that would work with the 3 argument file open.
here is the best that i could do :
my ($file,$wFH); if ( @ARGV ) { $file = shift; } else { $file = "STDOUT"; } my $message = "this is a test\n"; if ( $file ne "STDOUT" ) { open $wFH, ">", $file or die; } else { $wFH = *STDOUT; } print $wFH $message;
i was hoping to eliminate the IF block for opening a file. since this allows me to write to either STDOUT or a file, i have "generic" write stmts but not a "generic" open.
is there a better way? thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: assign File Handles to Scalar Variables (open >-)
by ikegami (Patriarch) on Jan 18, 2008 at 00:38 UTC | |
|
Re: assign File Handles to Scalar Variables (select)
by ikegami (Patriarch) on Jan 18, 2008 at 00:45 UTC | |
|
Re: assign File Handles to Scalar Variables
by starX (Chaplain) on Jan 18, 2008 at 00:41 UTC | |
|
Re: assign File Handles to Scalar Variables
by BrowserUk (Patriarch) on Jan 18, 2008 at 04:44 UTC |