DRVTiny

Std::Silencer.pm

Mar 12th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.48 KB | None | 0 0
  1. package Std::Silencer;
  2. use 5.16.1;
  3. use strict;
  4. use warnings;
  5.  
  6. use File::Spec;
  7.  
  8. sub on {
  9.     no strict 'refs';
  10.     my @sv = map {
  11.         open my $fh, q[>&] . $_;
  12.         open \*{$_}, File::Spec->devnull();
  13.         $fh
  14.     } qw/STDOUT STDERR/;
  15.     bless \@sv => ref($_[0]) || $_[0]
  16. }
  17.  
  18. sub off {
  19.     my $self = $_[0];
  20.     open $_ ? *STDERR : *STDOUT, q[>&], $self->[$_] for 0, 1;
  21.     @{$self} = ()
  22. }
  23.  
  24. sub DESTROY {
  25.     my $self = $_[0];
  26.     $self->off if @{$self};
  27. }
  28.  
  29. 1;
Add Comment
Please, Sign In to add comment