Redirection and encoding, broken or WAD?
BillSeurer
created: 2006-01-05 14:11:30
The following bit of code writes "yadda yadda" into a file via a redirected STDOUT. If STDOUT is not closed first the :encoding on the open is ignored (with Perl 5.8.6).
my $fooFile = "foo.out";                  
unlink $fooFile;
#close STDOUT;  # with this it works
open STDOUT, ">>:encoding(cp37)", $fooFile or die "Cannot redirect STDOUT\n";                     
select(STDOUT); $| = 1; 
print "yadda yadda\n";
close STDOUT;             
Is this working as expected or should it work without the close() in there?
Re: Redirection and encoding, broken or WAD?
created: 2006-01-05 14:33:53

Maybe I don't understand what you are trying to do ... but why not use a different file handle? You might consider redirecting STDOUT by using select:

  open (FOOFILE, ">>:encoding(cp37)") or die "Some error message: $!\n";
  select FOOFILE; 
  print "yadda yadda\n";  
  close(FOOFILE);
  select STDOUT;

No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re^2: Redirection and encoding, broken or WAD?
created: 2006-01-05 15:52:24
That doesn't capture other output (like from a system call). If you stick something like

system "ls";

in there in the original case the output from ls will go to the file (albeit not encoded) but using select it won't. I think they want this to work for all output going to the user's console device.

Re: Redirection and encoding, broken or WAD?
created: 2006-01-05 16:01:23
This is an excellent question, and I don't know the answer. The behaviour you describe is surprising, at least to me. The development version of perl exhibits the same behaviour. I wonder whether this is a known limitation, because there is a warning in [doc://perlfunc]:
Though if you try to re-open C or C as an "in memory"
file, you have to close it first:

    close STDOUT;
    open STDOUT, '>', \$variable or die "Can't open STDOUT: $!";
Of course this isn't talking about encodings, but I think it might be the same problem. Unless a PerlIO guru pops up here, I'd suggest asking p5p.

PS. If you just want a minimal demonstration script, you can delete most of your code:

# close STDOUT;  # with this it works
open STDOUT, ">:encoding(cp37)", "foo.out" or die "Cannot redirect STDOUT\n";
print "yadda yadda\n";
Re: Redirection and encoding, broken or WAD?
created: 2006-01-06 10:07:44
The STDOUT channel is already pre-opened at the beginning of the program, otherwise the default print statement wouldn't work. So it makes perfect sense to me that it should have to be closed before the filehandle can be re-used for something else.

perlmonks.org content © perlmonks.org and BillSeurer, DungeonKeeper, ptum, robin

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03