http://www.perlmonks.org?node_id=11128967

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

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]