File Coverage

blib/lib/Apache/Sling/Print.pm
Criterion Covered Total %
statement 68 70 97.1
branch 20 30 66.6
condition n/a
subroutine 14 14 100.0
pod 5 6 83.3
total 107 120 89.1


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             package Apache::Sling::Print;
4              
5 11     11   195 use 5.008001;
  11         36  
  11         450  
6 11     11   58 use strict;
  11         19  
  11         317  
7 11     11   53 use warnings;
  11         29  
  11         455  
8 11     11   63 use Carp;
  11         22  
  11         1016  
9 11     11   76 use Fcntl ':flock';
  11         25  
  11         1697  
10 11     11   14538 use File::Temp;
  11         2133578  
  11         1280  
11              
12             require Exporter;
13              
14 11     11   96 use base qw(Exporter);
  11         22  
  11         10758  
15              
16             our @EXPORT_OK = ();
17              
18             our $VERSION = '0.27';
19              
20             #{{{sub print_with_lock
21              
22             sub print_with_lock {
23 5     5 1 402 my ( $message, $file ) = @_;
24 5 100       19 if ( defined $file ) {
25 2         11 return print_file_lock( "$message", $file );
26             }
27             else {
28 3         21 return print_lock("$message");
29             }
30             }
31              
32             #}}}
33              
34             #{{{sub print_file_lock
35              
36             sub print_file_lock {
37 3     3 1 7 my ( $message, $file ) = @_;
38 3 50   1   150 if ( open my $out, '>>', $file ) {
  1         16  
  1         3  
  1         12  
39 3 100       2145 flock $out, LOCK_EX or croak q{Unable to obtain exclusive lock};
40 2 50       3 print {$out} $message . "\n" or croak q{Problem printing!};
  2         16  
41 2 50       105 flock $out, LOCK_UN or croak q{Problem releasing exclusive lock};
42 2 50       22 close $out or croak q{Problem closing!};
43             }
44             else {
45 0         0 croak "Could not open file: $file";
46             }
47 2         16 return 1;
48             }
49              
50             #}}}
51              
52             #{{{sub print_lock
53              
54             sub print_lock {
55 4     4 1 17 my ($message) = @_;
56 4         28 my ( $tmp_print_file_handle, $tmp_print_file_name ) =
57             File::Temp::tempfile();
58 4 50       598681 if ( open my $lock, '>>', $tmp_print_file_name ) {
59 4 50       48 flock $lock, LOCK_EX or croak q{Unable to obtain exclusive lock};
60 4 50       510 print $message . "\n" or croak q{Problem printing!};
61 4 50       39 flock $lock, LOCK_UN or croak q{Problem releasing exclusive lock};
62 4 50       60 close $lock or croak q{Problem closing!};
63 4         266 unlink($tmp_print_file_name);
64             }
65             else {
66 0         0 croak q(Could not open lock on temporary file!);
67             }
68 4         213 return 1;
69             }
70              
71             #}}}
72              
73             #{{{sub print_result
74              
75             sub print_result {
76 2     2 1 122 my ($object) = @_;
77 2         8 my $message = $object->{'Message'};
78 2 50       18 if ( $object->{'Verbose'} >= 1 ) {
79 2         7 $message .= "\n**** Status line was: ";
80 2         5 $message .= ${ $object->{'Response'} }->status_line;
  2         16  
81 2 100       52 if ( $object->{'Verbose'} >= 3 ) {
82 1         4 $message .= "\n**** Full Content of Response was: \n";
83 1         3 $message .= ${ $object->{'Response'} }->content;
  1         15  
84             }
85             }
86 2         25 print_with_lock( $message, $object->{'Log'} );
87 2         20 return 1;
88             }
89              
90             #}}}
91              
92             #{{{sub date_string
93              
94             sub date_string {
95 3     3 0 8 my ( $day_of_week, $month, $year_offset, $day_of_month, $hour, $minute,
96             $sec )
97             = @_;
98 3         17 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
99 3         9 my @week_days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
100 3 100       15 if ( $sec =~ /^[0-9]$/msx ) { $sec = "0$sec"; }
  1         4  
101 3 100       18 if ( $minute =~ /^[0-9]$/msx ) { $minute = "0$minute"; }
  1         3  
102 3         8 my $year = 1900 + $year_offset;
103             return
104 3         28 "$week_days[$day_of_week] $months[$month] $day_of_month $hour:$minute:$sec";
105             }
106              
107             #}}}
108              
109             #{{{sub date_time
110              
111             sub date_time {
112             (
113 1     1 1 45 my $sec,
114             my $minute,
115             my $hour,
116             my $day_of_month,
117             my $month,
118             my $year_offset,
119             my $day_of_week,
120             my $day_of_year,
121             my $daylight_savings
122             ) = localtime;
123 1         9 return date_string( $day_of_week, $month, $year_offset, $day_of_month,
124             $hour, $minute, $sec );
125             }
126              
127             #}}}
128              
129             1;
130              
131             __END__