File Coverage

blib/lib/Package/Watchdog/Sub/Forbidden.pm
Criterion Covered Total %
statement 35 35 100.0
branch 11 14 78.5
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 54 57 94.7


line stmt bran cond sub pod time code
1             package Package::Watchdog::Sub::Forbidden;
2 5     5   967 use strict;
  5         11  
  5         188  
3 5     5   23 use warnings;
  5         8  
  5         150  
4 5     5   43 use Package::Watchdog::Util;
  5         11  
  5         453  
5 5     5   28 use base 'Package::Watchdog::Sub';
  5         7  
  5         3835  
6 5     5   26 use Carp;
  5         10  
  5         1595  
7              
8             #{{{ POD
9              
10             =pod
11              
12             =head1 NAME
13              
14             Package::Watchdog::Sub::Forbidden - Object to manage a forbidden sub.
15              
16             =head1 DESCRIPTION
17              
18             Inherits methods from Package::Watchdog::Sub.
19              
20             =head1 METHODS
21              
22             =over 4
23              
24             =item new_sub()
25              
26             Returns the sub reference that will replace the original forbidden sub.
27              
28             =cut
29              
30             #}}}
31              
32             sub new_sub {
33 34     34 1 14081 my $self = shift;
34              
35             return sub {
36 36     36   44 my %seen;
37 36         50 for my $call ( @{ $self->tracker->stack }) {
  36         167  
38 34         210 my ( $watch, $watch_context ) = @$call;
39              
40 34         102 my $react = $watch->react;
41 34 50       192 next if $seen{ $react }++;
42 34 100       84 my $fatal = $react eq 'die' ? 'fatal' : undef;
43              
44 34         167 my $context = {
45             watch => $watch,
46             %$watch_context,
47             forbidden => $self,
48             forbidden_params => [ @_ ],
49             forbid => $self->tracker,
50             };
51              
52 34 100       89 if ( ref( $react )) {
53 12         52 $watch->do_react( %$context );
54             }
55             else {
56 22         73 $watch->warn( $context, $fatal );
57 22 100       370 croak( 'At least one watch with a die reaction has been triggered.' )
58             if $fatal;
59             }
60             }
61              
62 20         145 my $want = wantarray();
63 20         78 my @out = proper_return( $want, $self->original, @_ );
64 18 100       88 return @out if $want;
65 8 50       19 return shift( @out ) if defined( $want );
66 8 50       21 return @out if @out > 1;
67 8         27 return shift( @out );
68 34         225 };
69             }
70              
71             1;
72              
73             __END__