0

I have a perl script that requires a --log=<log> command line parameter. Obviously, this sends the script output to the filename specified. Without changing the script, is there any command-line value I can supply that would send output to STDOUT instead of a log file? When I supply "STDOUT", as you would probably expect, it sends the output to a file named "STDOUT".

Here is the relevant code:

use Getopt::Long;
...
my %C;
my $result = GetOptions("log=s" => \$C{log},
                        ...
                      );
...
my $log_file = $C{log};
...
my $log_fh;
if(open($log_fh, '>>', $log_file)) {
  print $log_fh $txt;
  close($log_fh);
} else {
  print STDERR "couldn't open log file $log_file\n";
}

I suspect the answer is no, but I was hoping someone wiser than me might have a solution.

Edit: It has been suggested that this is a duplicate of pass stdout as file name for command line util? . While there is some overlap, that question involves a command line utility in an unspecified language. My question relates specifically to Perl, and even more specifically to the code above, and so could potentially have a different answer.

  • 1
    Depending on OS, `/dev/stdout` may work. – Grinnz Sep 19 '19 at 18:49
  • 1
    And no, there are no Perl-specific ways to get around the filename arg in three-arg open. This is why it's considered safer than older options. – Grinnz Sep 19 '19 at 18:55
  • @Grinnz /dev/stdout is a brilliant idea. Unfortunately, it doesn't work on my system: `couldn't open log file /dev/stdout`. If I try directly from the command line with `echo "foo" > /dev/stdout`, I get `-bash: /dev/stdout: Permission denied`. `uname -a` returns `Linux myhost 2.6.32-754.17.1.el6.x86_64 #1 SMP Thu Jun 20 11:47:12 EDT 2019 x86_64 x86_64 x86_64 GNU/Linux` – Sagebrush Gardener Sep 19 '19 at 18:59

0 Answers0