File Coverage

blib/lib/Data/Printer/Scoped.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 41 41 100.0


line stmt bran cond sub pod time code
1             package Data::Printer::Scoped;
2             # ABSTRACT: silence Data::Printer except in a controlled scope
3             $Data::Printer::Scoped::VERSION = '0.001004';
4 2     2   49569 use strict;
  2         4  
  2         81  
5 2     2   12 use warnings;
  2         5  
  2         68  
6              
7 2     2   4102 use Data::Printer ();
  2         182003  
  2         60  
8 2     2   3353 use Import::Into;
  2         9451  
  2         177  
9 2     2   6483 use Context::Preserve;
  2         2181  
  2         137  
10 2     2   2465 use Class::Method::Modifiers qw(:all);
  2         7590  
  2         526  
11              
12 2     2   319 use base qw(Exporter);
  2         5  
  2         693  
13              
14             our @EXPORT = qw(scope);
15              
16             our $enabled = 0;
17              
18             install_modifier('Data::Printer', 'around', '_print_and_return', sub {
19             my $orig = shift;
20              
21             # noop unless enabled.
22             return $enabled ? $orig->(@_) : ();
23             });
24              
25             sub import {
26 2     2   379 shift->export_to_level(1);
27 2         255 Data::Printer->import::into(1);
28             }
29              
30             # we only blanket disable Data::Printer if a scope() call has been made.
31             sub scope(&) {
32 2     2 1 30 my ($code) = @_;
33              
34 2         5 $enabled = 0;
35              
36 2     2   25 return preserve_context { $enabled = 1; $code->() }
  2         6  
37 2     2   39 after => sub { $enabled = 0 };
  2         1335  
38             }
39              
40             1;
41              
42             __END__