Dispatch/Email.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 94 CC 5 SCAR 14.2
CC
5
Cov
0%
new
6.0
log_message
6.0
flush
6.0
CRAP 4.1
1 package Log::Dispatch::Email;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '2.71';
7  
8 use Devel::GlobalDestruction qw( in_global_destruction );
9 use Log::Dispatch::Types;
10 use Params::ValidationCompiler qw( validation_for );
11  
12 use base qw( Log::Dispatch::Output );
13  
14 {
15 # need to untaint this value
16 my ($program) = $0 =~ /(.+)/;
17  
18 my $validator = validation_for(
19 params => {
20 subject => {
21 type => t('Str'),
22 default => "$program: log email",
23 },
24 to => { type => t('ArrayOfAddresses') },
25 from => {
26 type => t('SimpleStr'),
27 optional => 1,
28 },
29 buffered => {
30 type => t('Bool'),
31 default => 1,
32 },
33 },
34 slurpy => 1,
35 );
36  
37 sub new {
38 my $class = shift;
39 my %p = $validator->(@_);
40  
41 my $self = bless {
42 subject => delete $p{subject},
43 to => delete $p{to},
44 from => delete $p{from},
45 buffered => delete $p{buffered},
46 }, $class;
47 $self->{buffer} = [] if $self->{buffered};
48  
49 $self->_basic_init(%p);
50  
51 return $self;
52 }
53 }
54  
55 sub log_message {
56 my $self = shift;
57 my %p = @_;
58  
59 if ( $self->{buffered} ) {
60 push @{ $self->{buffer} }, $p{message};
61 }
62 else {
63 $self->send_email(@_);
64 }
65 }
66  
67 sub send_email {
68 my $self = shift;
69 my $class = ref $self;
70  
71 die "The send_email method must be overridden in the $class subclass";
72 }
73  
74 sub flush {
75 my $self = shift;
76  
77 if ( $self->{buffered} && @{ $self->{buffer} } ) {
78 my $message = join q{}, @{ $self->{buffer} };
79  
80 $self->send_email( message => $message );
81 $self->{buffer} = [];
82 }
83 }
84  
85 sub DESTROY {
86 my $self = shift;
87  
88 if ( in_global_destruction()
89 && $self->{buffered}
90 && @{ $self->{buffer} } ) {
91  
92 my $name = $self->name();
93 my $class = ref $self;
94 my $message
95 = "Log messages for the $name output (a $class object) remain unsent but the program is terminating.\n";
96 $message .= "The messages are:\n";
97 $message .= " $_\n" for @{ $self->{buffer} };
98  
99 warn $message;
100 }
101 else {
102 $self->flush();
103 }
104 }
105  
106 1;
107  
108 # ABSTRACT: Base class for objects that send log messages via email
109  
110 __END__