Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silencing STDERR and warnings

Tags:

perl

How does warn 4 differ from print STDERR 4?

perl -e 'local *STDERR; warn 4'

(output still goes to STDERR)

perl -e 'local *STDERR; print STDERR 4'

(no output here)

like image 836
mpapec Avatar asked Dec 15 '22 05:12

mpapec


2 Answers

  • warn triggers $SIG{__WARN__}.
  • warn doesn't use $\ or $,.
  • And warn apparently uses the file handle in the original STDERR, as you've demonstrated[1].

  1. Not quite. Your code could also demonstrate that warn uses fd 2 directly, but that's disproven by

    close(STDOUT);
    close(STDERR);
    open(STDERR, '>file');
    warn(fileno(STDERR)."abc");  # 1abc
    
like image 148
ikegami Avatar answered Jan 14 '23 11:01

ikegami


You haven't silenced the STDERR handle yet. In order to really silence it, you need to say:

perl -e 'local *STDERR; open(STDERR, ">/dev/null") or die $!; warn 4'

perldoc perlvar tells:

As the 'IGNORE' hook is not supported by __WARN__, you can disable warnings using the empty subroutine:

 local $SIG{__WARN__} = sub {};
like image 38
devnull Avatar answered Jan 14 '23 12:01

devnull