First, get the filename from the command line (using the Getopt::Std or Getopt::Long module, perhaps.)
# Redirect STDOUT. if ($filename) { open(STDOUT, '>', $filename) or die("Can't open redirect STDOUT: $!\n"); } print(...); # Goes to file if one was specified, else STDOUT.
-or-
# Use select. if ($filename) { open(FILE, '>', $filename) or die("Can't open $filename: $!\n"); select(FILE); } print(...); # Goes to file if one was specified, else STDOUT.
-or-
# If you want to output to STDOUT and to the file, # create a handle which may point to STDOUT. if ($filename) { open(FILE, '>', $filename) or die("Can't open $filename: $!\n"); } else { open(FILE, ">&STDOUT"); or die("Can't dup STDOUT: $!\n"); } print FILE (...); # Goes to file if one was specified, else STDOUT. print(...); # Goes to STDOUT.
In reply to Re: Filehander Question
by ikegami
in thread Filehander Question
by EchoAngel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |