File Coverage

blib/lib/Tie/Handle/Filter/Output/Timestamp/EveryLine.pm
Criterion Covered Total %
statement 30 30 100.0
branch 1 2 50.0
condition 2 5 40.0
subroutine 9 9 100.0
pod n/a
total 42 46 91.3


line stmt bran cond sub pod time code
1             package Tie::Handle::Filter::Output::Timestamp::EveryLine;
2              
3             # ABSTRACT: prepend every line of filehandle output with a timestamp
4              
5 1     1   19595 use 5.008;
  1         2  
6 1     1   3 use strict;
  1         1  
  1         14  
7 1     1   2 use warnings;
  1         1  
  1         19  
8 1     1   2 use base 'Tie::Handle::Filter';
  1         1  
  1         360  
9 1     1   4 use English '-no_match_vars';
  1         1  
  1         4  
10 1     1   280 use POSIX 'strftime';
  1         1  
  1         4  
11             our $VERSION = '0.010'; # TRIAL
12              
13             #pod =head1 SYNOPSIS
14             #pod
15             #pod use Tie::Handle::Filter::Output::Timestamp::EveryLine;
16             #pod tie *STDOUT, 'Tie::Handle::Filter::Output::Timestamp::EveryLine', *STDOUT;
17             #pod
18             #pod print "Everything I print will be prepended with a timestamp.\n";
19             #pod print <<'END_OUTPUT';
20             #pod Even multi-line output will have every line prepended.
21             #pod Including this one.
22             #pod END_OUTPUT
23             #pod
24             #pod =cut
25              
26             #pod =head1 DESCRIPTION
27             #pod
28             #pod This class may be used with Perl's L function to
29             #pod prepend all output with a timestamp, optionally formatted according to
30             #pod the L|POSIX/strftime> function. Unlike
31             #pod L|Tie::Handle::Filter::Output::Timestamp>,
32             #pod I line gets a timestamp, rather than just the beginning of
33             #pod strings given to L|perlfunc/print>,
34             #pod L|perlfunc/printf>, L|perlfunc/syswrite>, and
35             #pod L|perlfunc/say> (in Perl > v5.10).
36             #pod
37             #pod =head1 BUGS AND LIMITATIONS
38             #pod
39             #pod Because the date and time format is specified using
40             #pod L|POSIX/strftime>, portable code should restrict itself to
41             #pod formats using ANSI C89 specifiers.
42             #pod
43             #pod =cut
44              
45             my $NEWLINE = $PERL_VERSION lt 'v5.10'
46             ? '(?>\x0D\x0A|\n)' ## no critic (RequireInterpolationOfMetachars)
47             : '\R';
48              
49             #pod =method TIEHANDLE
50             #pod
51             #pod Invoked by the command
52             #pod C.
53             #pod You may also specify a L|POSIX/strftime> string as an
54             #pod additional parameter to format the timestamp; by default the format is
55             #pod C<%x %X >, which is the local representation of the date and time
56             #pod followed by a space.
57             #pod
58             #pod =cut
59              
60             sub TIEHANDLE {
61 1     1   214 my ( $class, $handle_glob, $format ) = @_;
62 1   50     7 return $class->SUPER::TIEHANDLE( $handle_glob,
63             _filter_closure( $format || '%x %X ' ) );
64             }
65              
66             sub _filter_closure {
67 1     1   2 my $format = shift;
68 1         1 my $string_beginning = 1;
69             return sub {
70 2     2   5 my $string = join q() => @_;
71 2         24 $string
72 3         136 =~ s/ ($NEWLINE) (?=.) / $1 . strftime($format, localtime) /egmsx;
73 2 50       55 if ($string_beginning) {
74 2         5 $string =~ s/ \A / strftime($format, localtime) /emsx;
  2         4  
75             }
76 2   33     89 $string_beginning = $string =~ / $NEWLINE \z/msx
77             || $OUTPUT_RECORD_SEPARATOR eq "\n";
78 2         19 return $string;
79 1         13 };
80             }
81              
82             1;
83              
84             __END__