Filter.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 61.1% 11 / 18 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 20 / 20 Podpod n/a totaltotal 83.3% 90 / 108 CC 14 SCAR 16.8
CC
14
Cov
84%
_get_filehandle_open_mode
14.9
TIEHANDLE
4.1
WRITE
3.0
CRAP 5.3
↑ Summary Filter/Output/Timestamp/EveryLine.pm »
1 package Tie::Handle::Filter;
2  
3 # ABSTRACT: [DEPRECATED] filters filehandle output through a coderef
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod use Tie::Handle::Filter;
8 #pod
9 #pod # prefix output to STDERR with standard Greenwich time
10 #pod BEGIN {
11 #pod tie *STDERR, 'Tie::Handle::Filter', *STDERR,
12 #pod sub { scalar(gmtime) . ': ', @_ };
13 #pod }
14 #pod
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod B<DEPRECATION NOTICE:> This module distribution is deprecated in favor
18 #pod of L<Text::OutputFilter|Text::OutputFilter>, which is more robust while
19 #pod being functionally identical, or
20 #pod L<PerlIO::via::dynamic|PerlIO::via::dynamic>, which uses a different
21 #pod mechanism that may offer better performance.
22 #pod
23 #pod This is a small module for changing output when it is sent to a given
24 #pod file handle. By default it passes everything unchanged, but when
25 #pod provided a code reference, that reference is passed the string being
26 #pod sent to the tied file handle and may return a transformed result.
27 #pod
28 #pod =cut
29  
30 8 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶use base 'Tie::Handle';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 8 ▶use FileHandle::Fmode ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 our $VERSION = '0.011';
38  
39 #pod =head1 DIAGNOSTICS
40 #pod
41 #pod Wherever possible this module attempts to emulate the built-in functions
42 #pod it ties, so it will return values as expected from whatever function is
43 #pod called. Certain operations may also L<C<croak>|Carp> (throw a fatal
44 #pod exception) if they fail, such as aliasing the file handle during a
45 #pod L<C<tie>|perlfunc/tie> or attempting to perform an
46 #pod L<unsupported operation|/"BUGS AND LIMITATIONS"> on a tied file handle.
47 #pod
48 #pod =cut
49  
50 22 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=4 SCAR=14.1
51 22 my ( $class, $handle_glob, $writer_ref ) = @_;
52  
53 ## no critic (InputOutput::RequireBriefOpen)
54 22 ▶ open my $fh, _get_filehandle_open_mode($handle_glob) . q(&=), $handle_glob
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, _get_filehandle_open_mode($handle_glob) . '&=', $handle_glob or croak($Tie::Handle::Filter::OS_ERROR) 22 0 22
55 or croak $OS_ERROR;
56  
57 return bless {
58 filehandle => $fh,
59 writer => (
60 ( defined $writer_ref and 'CODE' eq ref $writer_ref )
61 ? $writer_ref
62 2 ▶ : sub { return @_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
63 22 ▶ ),
Branch: 1 branch
BranchTrueFalseTotal
defined $writer_ref && 'CODE' eq ref $writer_ref ? : 10 12 22
Condition66%2/3
defined $writer_ref and "CODE" eq ref $writer_ref
!ll&&!rl&&r
12010
MC/DC50%1/2
defined $writer_ref and "CODE" eq ref $writer_ref
defined $writer_ref "CODE" eq ref $writer_ref
Truth table66%2/3
A and B
A= defined $writer_refB= "CODE" eq ref $writer_ref
ABresult
0X0
100
111
64 }, $class;
65 }
66  
67 22 ▶sub _get_filehandle_open_mode {
Subroutine _get_filehandle_open_mode: called CC=9 SCAR=27.0
68 22 my $fh = shift;
69 22 for ($fh) {
70 22 ▶ return '>>' if is_WO($_) and is_A($_);
Branch: 1 branch
BranchTrueFalseTotal
if is_WO($_) and is_A($_) 0 22 22
Condition33%1/3
is_WO($_) and is_A($_)
!ll&&!rl&&r
2200
MC/DC0%0/2
is_WO($_) and is_A($_)
is_WO($_) is_A($_)
Truth table33%1/3
A and B
A= is_WO($_)B= is_A($_)
ABresult
0X0
100
111
71 22 ▶ return '>' if is_WO($_);
Branch: 1 branch
BranchTrueFalseTotal
if is_WO($_) 0 22 22
72 22 ▶ return '<' if is_RO($_);
Branch: 1 branch
BranchTrueFalseTotal
if is_RO($_) 0 22 22
73 22 ▶ return '+>>' if is_RW($_) and is_A($_);
Branch: 1 branch
BranchTrueFalseTotal
if is_RW($_) and is_A($_) 0 22 22
Condition33%1/3
is_RW($_) and is_A($_)
!ll&&!rl&&r
0220
MC/DC0%0/2
is_RW($_) and is_A($_)
is_RW($_) is_A($_)
Truth table33%1/3
A and B
A= is_RW($_)B= is_A($_)
ABresult
0X0
100
111
74 22 ▶ return '+<' if is_RW($_);
Branch: 1 branch
BranchTrueFalseTotal
if is_RW($_) 22 0 22
75 }
76 0 return '+>';
77 }
78  
79 ## no critic (Subroutines::RequireArgUnpacking)
80 ## no critic (Subroutines::RequireFinalReturn)
81  
82 #pod =method PRINT
83 #pod
84 #pod All arguments to L<C<print>|perlfunc/print> and L<C<say>|perlfunc/say>
85 #pod directed at the tied file handle are passed to the user-defined
86 #pod function, and the result is then passed to L<C<print>|perlfunc/print>.
87 #pod
88 #pod =cut
89  
90 9 ▶sub PRINT {
Subroutine PRINT: called CC=1 SCAR=0.0
91 9 my $self = shift;
92 ## no critic (InputOutput::RequireCheckedSyscalls)
93 9 print { $self->{filehandle} } $self->{writer}->(@_);
94 }
95  
96 #pod =method PRINTF
97 #pod
98 #pod The second and subsequent arguments to L<C<printf>|perlfunc/printf>
99 #pod (i.e., everything but the format string) directed at the tied file
100 #pod handle are passed to the user-defined function, and the result is then
101 #pod passed preceded by the format string to L<C<printf>|perlfunc/printf>.
102 #pod
103 #pod Please note that this does not include calls to
104 #pod L<C<sprintf>|perlfunc/sprintf>.
105 #pod
106 #pod =cut
107  
108 2 ▶sub PRINTF {
Subroutine PRINTF: called CC=1 SCAR=0.0
109 2 my ( $self, $format ) = splice @_, 0, 2;
110 2 printf { $self->{filehandle} } $format, $self->{writer}->(@_);
111 }
112  
113 #pod =method WRITE
114 #pod
115 #pod The first argument to L<C<syswrite>|perlfunc/syswrite> (i.e., the buffer
116 #pod scalar variable) directed at the tied file handle is passed to the
117 #pod user-defined function, and the result is then passed along with the
118 #pod optional second and third arguments (i.e., length of data in bytes and
119 #pod offset within the string) to L<C<syswrite>|perlfunc/syswrite>.
120 #pod
121 #pod Note that if you do not provide a length argument to
122 #pod L<C<syswrite>|perlfunc/syswrite>, it will be computed from the result of
123 #pod the user-defined function. However, if you do provide a length (and
124 #pod possibly offset), they will be relative to the results of the
125 #pod user-defined function, not the input.
126 #pod
127 #pod =cut
128  
129 3 ▶sub WRITE {
Subroutine WRITE: called CC=3 SCAR=11.1
130 3 my ( $self, $original ) = splice @_, 0, 2;
131 3 my $buffer = ( $self->{writer}->($original) )[0];
132 3 ▶ syswrite $self->{filehandle}, $buffer,
Branch: 2 branches
BranchTrueFalseTotal
defined $_[0] ? : 3 0 3
defined $_[1] ? : 1 2 3
133 ( defined $_[0] ? $_[0] : length $buffer ),
134 ( defined $_[1] ? $_[1] : 0 );
135 }
136  
137 10 ▶sub CLOSE {
Subroutine CLOSE: called CC=1 SCAR=0.0
138 10 my $self = shift;
139 ## no critic (InputOutput::RequireCheckedSyscalls)
140 ## no critic (InputOutput::RequireCheckedClose)
141 10 close $self->{filehandle};
142 }
143  
144 #pod =head1 BUGS AND LIMITATIONS
145 #pod
146 #pod If your function needs to know what operation was used to call it,
147 #pod consider using C<(caller 1)[3]> to determine the method used to call
148 #pod it, which will return C<Tie::Handle::Filter::PRINT>,
149 #pod C<Tie::Handle::Filter::PRINTF>, or C<Tie::Handle::Filter::WRITE> per
150 #pod L<perltie/"Tying FileHandles">.
151 #pod
152 #pod Currently this module is biased towards write-only file handles, such as
153 #pod C<STDOUT>, C<STDERR>, or ones used for logging. It does not (yet) define
154 #pod the following methods and their associated functions, so don't do them
155 #pod with file handles tied to this class.
156 #pod
157 #pod =head2 READ
158 #pod
159 #pod =over
160 #pod
161 #pod =item L<C<read>|perlfunc/read>
162 #pod
163 #pod =item L<C<sysread>|perlfunc/sysread>
164 #pod
165 #pod =back
166 #pod
167 #pod =head2 READLINE
168 #pod
169 #pod =over
170 #pod
171 #pod =item L<C<E<lt>HANDLEE<gt>>|perlop/"I/O Operators">
172 #pod
173 #pod =item L<C<readline>|perlfunc/readline>
174 #pod
175 #pod =back
176 #pod
177 #pod =head2 GETC
178 #pod
179 #pod =over
180 #pod
181 #pod =item L<C<getc>|perlfunc/getc>
182 #pod
183 #pod =back
184 #pod
185 #pod =head2 OPEN
186 #pod
187 #pod =over
188 #pod
189 #pod =item L<C<open>|perlfunc/open> (e.g., re-opening the file handle)
190 #pod
191 #pod =back
192 #pod
193 #pod =head2 BINMODE
194 #pod
195 #pod =over
196 #pod
197 #pod =item L<C<binmode>|perlfunc/binmode>
198 #pod
199 #pod =back
200 #pod
201 #pod =head2 EOF
202 #pod
203 #pod =over
204 #pod
205 #pod =item L<C<eof>|perlfunc/eof>
206 #pod
207 #pod =back
208 #pod
209 #pod =head2 TELL
210 #pod
211 #pod =over
212 #pod
213 #pod =item L<C<tell>|perlfunc/tell>
214 #pod
215 #pod =back
216 #pod
217 #pod =head2 SEEK
218 #pod
219 #pod =over
220 #pod
221 #pod =item L<C<seek>|perlfunc/seek>
222 #pod
223 #pod =back
224 #pod
225 #pod =cut
226  
227 5 ▶my $unimplemented_ref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
229 5 my $package = ( caller 0 )[0];
230 5 my $method = ( caller 1 )[3];
231 5 $method =~ s/\A ${package} :: //xms;
232 5 croak "$package doesn't define a $method method";
233 8 };
234  
235 1 ▶sub OPEN { $unimplemented_ref->() }
Subroutine OPEN: called CC=1 SCAR=0.0
236 1 ▶sub BINMODE { $unimplemented_ref->() }
Subroutine BINMODE: called CC=1 SCAR=0.0
237 1 ▶sub EOF { $unimplemented_ref->() }
Subroutine EOF: called CC=1 SCAR=0.0
238 1 ▶sub TELL { $unimplemented_ref->() }
Subroutine TELL: called CC=1 SCAR=0.0
239 1 ▶sub SEEK { $unimplemented_ref->() }
Subroutine SEEK: called CC=1 SCAR=0.0
240  
241 #pod =head1 SEE ALSO
242 #pod
243 #pod =over
244 #pod
245 #pod =item L<Tie::Handle::Filter::Output::Timestamp|Tie::Handle::Filter::Output::Timestamp>
246 #pod
247 #pod Prepends filehandle output with a timestamp, optionally formatted via
248 #pod L<C<strftime>|POSIX/strftime>.
249 #pod
250 #pod =item L<Tie::Handle::Filter::Output::Timestamp::EveryLine|Tie::Handle::Filter::Output::Timestamp::EveryLine>
251 #pod
252 #pod Prepends every line of filehandle output with a timestamp, optionally
253 #pod formatted via L<C<strftime>|POSIX/strftime>.
254 #pod
255 #pod =back
256 #pod
257 #pod =cut
258  
259 8 1;
260  
261 __END__
↑ Summary Filter/Output/Timestamp/EveryLine.pm »