In an ancient part of the code at work, we use
open my $output, '>-' or die $!;

Perl::Critic complains about it and wants us to replace it with a three argument variant. But, what's the 3-arg variant of >-?

I thought

open my $output, '>&', *STDOUT or die $!;
might be the same, but it isn't, cf.
open my $output, '>-' or die $!; close *STDOUT; print {$output} "This fails with Bad file descriptor";
versus
open my $output, '>&', *STDOUT or die $!; close *STDOUT; print {$output} "This prints OK\n";

Then I tried with

my $output = *STDOUT;
but it's different, too:
close STDOUT; print {$output} 'Besides Bad file descriptor, this will also warn "print() on clos +ed filehandle STDOUT"';

Is there a three-argument version of open that behaves the same as '>-'?

Update: Here's a SSCCE. Closing STDOUT is a global state, so call this with an argument (1, 2, 3, 4) to test a particular implementation.

#!/usr/bin/perl use strict; use warnings; sub original { open my $output, '>-' or die $!; $output } sub assign { my $output = *STDOUT; $output } sub dup { open my $output, '>&', *STDOUT or die $!; $output } sub fn { open my $output, '>>&=', *STDOUT->fileno or die $!; $output } use Test::More; sub test { my ($open) = @_; my @W; local $SIG{__WARN__} = sub { push @W, @_ }; my $output = $open->(); close *STDOUT; my $v = print {$output} "abc\n"; isnt $v, 1, 'fails'; like $!, qr/Bad file descriptor/, 'Exception'; is scalar @W, 0, 'no warnings'; chomp, diag "($_)" for @W; done_testing(); } my %dispatch = (1 => \&original, 2 => \&assign, 3 => \&dup, 4 => \&fn); my $what = shift; my $code = $dispatch{$what} or die 'Not associated'; test($code);

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Three argument version of open '>-' (updated) by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.