cmre
cmre

Reputation: 83

Open filehandle or assign stdout

I'm working in a program where the user can pass a -o file option, and output should be then directed to that file. Otherwise, it should go to stdout.

To retrieve the option I'm using the module getopt long, and that's not the problem. The problem is that I want to create a file handle with that file or assign stdout to it if the option was not set.

if ($opt) {
    open OUTPUT, ">", $file;
} else {
    open OUTPUT, # ???
}

That's because this way, later in my code I can just:

print OUTPUT "...";

Without worrying if OUTPUT is stdout or a file the user specified. Is this possible? If I'm doing a bad design here, please let me know.

Upvotes: 8

Views: 3894

Answers (3)

Ale
Ale

Reputation: 997

A constant item such as OUTPUT cannot be assigned. Using a variable such as $output works better. For example:

my ($output, $display_filename);
if ($opt)
{
    if ($opt eq '-')
    {
        $display_filename = 'stdout';
        $output = *STDOUT;
    }
    else
    {
        $display_filename = $opt;
        open($output, '>', $opt) or
            die("Cannot open $opt for writing: $!\n");
    }
}

That way the program can print to standard output and/or to an output file:

print $output "This might go to a file\n";
print "Data written to $display_filename\n" if ($verbose);

Upvotes: 0

TLP
TLP

Reputation: 67900

This would be a good example on how to use select.

use strict;
use warnings;
use autodie;

my $fh;
if ($opt) {
    open $fh, '>', $file;
    select $fh;
}

print "This goes to the file if $opt is defined, otherwise to STDOUT."

Upvotes: 7

Mat
Mat

Reputation: 206689

Look at the open documentation. The easiest is to reopen STDOUT itself and not use a filehandle in your code.

if ($opt) {
    open(STDOUT, ">", $file);
}
...

print "this goes to $file or STDOUT\n";

(Add some error checking of course.)

Upvotes: 4

Related Questions