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
might be the same, but it isn't, cf.open my $output, '>&', *STDOUT or die $!;
versusopen my $output, '>-' or die $!; close *STDOUT; print {$output} "This fails with Bad file descriptor";
open my $output, '>&', *STDOUT or die $!; close *STDOUT; print {$output} "This prints OK\n";
Then I tried with
but it's different, too:my $output = *STDOUT;
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]
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Three argument version of open '>-' (de-critic)
by LanX (Saint) on Mar 02, 2021 at 02:16 UTC | |
by jcb (Parson) on Mar 02, 2021 at 02:41 UTC | |
Re: Three argument version of open '>-'
by Fletch (Bishop) on Mar 01, 2021 at 22:23 UTC | |
Re: Three argument version of open '>-'
by LanX (Saint) on Mar 01, 2021 at 22:21 UTC | |
by choroba (Cardinal) on Mar 01, 2021 at 23:06 UTC | |
by LanX (Saint) on Mar 01, 2021 at 23:24 UTC | |
Re: Three argument version of open '>-'
by LanX (Saint) on Mar 01, 2021 at 21:55 UTC |
Back to
Seekers of Perl Wisdom