Netsync/Scribe.pm

filter: Statementstmt 65.3% 32 / 49 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 83.3% 10 / 12 Podpod 100% 2 / 2 totaltotal 52.3% 44 / 84 CC 11 SCAR 30.3
CC
11
Cov
48%
note
56.0
timestamp
12.0
END
6.8
CRAP 20.8
« Netsync/Network.pm ↑ Summary Netsync/SNMP.pm »
1 package App::Netsync::Scribe; #XXX Log::Message?
2  
3 =head1 NAME
4  
5 App::Netsync::Scribe - I/O framework
6  
7 =head1 DESCRIPTION
8  
9 This package handles I/O automatically.
10  
11 =head1 SYNOPSIS
12  
13 use App::Netsync::Scribe;
14  
15 print timestamp;
16 note('foo.txt','Don't stamp this note.',0);
17  
18 =cut
19  
20  
21 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use autodie; #XXX Is autodie adequate?
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 our ($SCRIPT,$VERSION);
32 1 our %files;
33  
34 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ($SCRIPT) = fileparse ($0,"\.[^.]*");
36 1 ($VERSION) = version->declare('v4.0.0');
37  
38 1 require Exporter;
39 1 our @ISA = ('Exporter');
40 1 our @EXPORT_OK = ('note','timestamp');
41 }
42  
43  
44 =head1 METHODS
45  
46 =head2 timestamp
47  
48 returns an string representing the current time
49  
50 B<Arguments>
51  
52 I<( [ $format ] )>
53  
54 =over 3
55  
56 =item format
57  
58 the POSIX time-string format to use
59  
60 default: '%Y-%m-%d-%H:%M:%S'
61  
62 =back
63  
64 =cut
65  
66 0 ▶sub timestamp {
Subroutine timestamp: not called CC=3 SCAR=24.8
Pod documented
67 0 ▶ warn 'too many arguments' if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
68 0 my ($format) = @_;
69 0 ▶ $format //= '%Y:%m:%d:%H:%M:%S';
Condition0%0/2
$format //= "%Y:%m:%d:%H:%M:%S"
l!l
00
MC/DC0%0/1
$format //= "%Y:%m:%d:%H:%M:%S"
$format
Truth table0%0/2
A
A= $format
Aresult
11
00
70  
71 0 my $timestamp = POSIX::strftime($format,localtime);
72 0 return $timestamp;
73 }
74  
75  
76 =head2 note
77  
78 writes to a specified file and optionally including a timestamp
79  
80 B<Arguments>
81  
82 I<( $file , $note [, $stamp [, $mode ] ] )>
83  
84 =over 3
85  
86 =item file
87  
88 the file to write to
89  
90 =item note
91  
92 the string to write
93  
94 =item stamp
95  
96 whether to timestamp the note
97  
98 default: 1
99  
100 =item mode
101  
102 the method to use when opening the file (if it hasn't been opened already)
103  
104 default: '>>'
105  
106 =back
107  
108 =cut
109  
110 0 ▶sub note {
Subroutine note: not called CC=7 SCAR=40.3
Pod documented
111 0 ▶ warn 'too few arguments' if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 0 0
112 0 ▶ warn 'too many arguments' if @_ > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 4 0 0 0
113 0 my ($file,$note,$stamp,$mode) = @_;
114 0 ▶ $stamp //= 1;
Condition0%0/2
$stamp //= 1
l!l
00
MC/DC0%0/1
$stamp //= 1
$stamp
Truth table0%0/2
A
A= $stamp
Aresult
11
00
115 0 ▶ $mode //= '>>';
Condition0%0/2
$mode //= ">>"
l!l
00
MC/DC0%0/1
$mode //= ">>"
$mode
Truth table0%0/2
A
A= $mode
Aresult
11
00
116  
117 0 ▶ open ($files{$file},$mode,$file) unless defined $files{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $App::Netsync::Scribe::files{$file} 0 0 0
118 0 ▶ print {$files{$file}} timestamp.' ' if $stamp;
Branch: 1 branch
BranchTrueFalseTotal
if $stamp 0 0 0
119 0 say {$files{$file}} $note;
120 0 return 1;
121 }
122  
123  
124 1 ▶END {
Subroutine END: called CC=3 SCAR=19.2
125 1 foreach my $file (keys %files) {
126 0 ▶ close $files{$file} if defined $files{$file};
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::Netsync::Scribe::files{$file} 0 0 0
127 }
128 }
129  
130  
131 =head1 AUTHOR
132  
133 David Tucker, C<< <dmtucker at ucsc.edu> >>
134  
135 =head1 BUGS
136  
137 Please report any bugs or feature requests to C<bug-netsync at rt.cpan.org>, or through
138 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-Netsync>. I will be notified, and then you'll
139 automatically be notified of progress on your bug as I make changes.
140  
141 =head1 SUPPORT
142  
143 You can find documentation for this module with the perldoc command.
144  
145 perldoc App::Netsync
146  
147 You can also look for information at:
148  
149 =over 4
150  
151 =item * RT: CPAN's request tracker (report bugs here)
152  
153 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-Netsync>
154  
155 =item * AnnoCPAN: Annotated CPAN documentation
156  
157 L<http://annocpan.org/dist/App-Netsync>
158  
159 =item * CPAN Ratings
160  
161 L<http://cpanratings.perl.org/d/App-Netsync>
162  
163 =item * Search CPAN
164  
165 L<http://search.cpan.org/dist/App-Netsync/>
166  
167 =back
168  
169 =head1 LICENSE
170  
171 Copyright 2013 David Tucker.
172  
173 This program is free software; you can redistribute it and/or modify it
174 under the terms of either: the GNU General Public License as published
175 by the Free Software Foundation; or the Artistic License.
176  
177 See L<http://dev.perl.org/licenses/> for more information.
178  
179 =cut
180  
181  
182 1 1;
« Netsync/Network.pm ↑ Summary Netsync/SNMP.pm »