Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Redirection and encoding, broken or WAD?

by BillSeurer (Sexton)
on Jan 05, 2006 at 19:11 UTC ( #521318=perlquestion: print w/replies, xml ) Need Help??

BillSeurer has asked for the wisdom of the Perl Monks concerning the following question:

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 STD +OUT\n"; select(STDOUT); $| = 1; print "yadda yadda\n"; close STDOUT;

Is this working as expected or should it work without the close() in there?

I had the person who found this add close()s so it's no big deal but I am just curious as to what is correct.

Replies are listed 'Best First'.
Re: Redirection and encoding, broken or WAD?
by robin (Chaplain) on Jan 05, 2006 at 21:01 UTC
    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 perlfunc:
    Though if you try to re-open C<STDOUT> or C<STDERR> 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 STD +OUT\n"; print "yadda yadda\n";
Re: Redirection and encoding, broken or WAD?
by ptum (Priest) on Jan 05, 2006 at 19:33 UTC

    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
      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?
by DungeonKeeper (Novice) on Jan 06, 2006 at 15:07 UTC
    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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://521318]
Approved by BrowserUk
Front-paged by BrowserUk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2022-10-04 10:16 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (16 votes). Check out past polls.