I want to redirect STDOUT to a FileHandle and I tried this:
#! /usr/bin/perl -w
use FileHandle;
$fh=FileHandle->new(">blarg");
open(STDOUT, ">&$fh") or die "Argh: $!";
print "foo bar\nbaz\n";
$fh->close();
It gives me
Argh: Invalid argument at chb.pl line 6.
After intense manual study I came across this solution:
#! /usr/bin/perl -w
use FileHandle;
$fh=FileHandle->new(">blarg");
open(STDOUT, sprintf(">&=%d", $fh->fileno()));
print "foo bar\nbaz\n";
$fh->close();
It works, but it looks
really ugly. My Question is: is there a cleaner way to do this ? Have I missed some manpage ?
- chb