File Coverage

blib/lib/Tie/Handle/Filter/Output/Timestamp.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition 1 2 50.0
subroutine 7 7 100.0
pod n/a
total 26 27 96.3


line stmt bran cond sub pod time code
1             package Tie::Handle::Filter::Output::Timestamp;
2              
3             # ABSTRACT: prepend filehandle output with a timestamp
4              
5 1     1   21314 use 5.008;
  1         3  
6 1     1   3 use strict;
  1         1  
  1         15  
7 1     1   2 use warnings;
  1         1  
  1         18  
8 1     1   3 use base 'Tie::Handle::Filter';
  1         0  
  1         359  
9 1     1   5 use POSIX 'strftime';
  1         1  
  1         5  
10             our $VERSION = '0.011';
11              
12             #pod =head1 SYNOPSIS
13             #pod
14             #pod use Tie::Handle::Filter::Output::Timestamp;
15             #pod tie *STDOUT, 'Tie::Handle::Filter::Output::Timestamp', *STDOUT;
16             #pod
17             #pod print "Everything I print will be prepended with a timestamp.\n";
18             #pod print <<'END_OUTPUT';
19             #pod The first line of a multi-line string will be prepended.
20             #pod Subsequent lines will not.
21             #pod END_OUTPUT
22             #pod
23             #pod =head1 DESCRIPTION
24             #pod
25             #pod This class may be used with Perl's L function to
26             #pod prepend all output with a timestamp, optionally formatted according to
27             #pod the L|POSIX/strftime> function. Only the beginning of
28             #pod strings given to L|perlfunc/print>,
29             #pod L|perlfunc/printf>, L|perlfunc/syswrite>, and
30             #pod L|perlfunc/say> (in Perl > v5.10) get timestamps.
31             #pod
32             #pod =head1 BUGS AND LIMITATIONS
33             #pod
34             #pod Because the date and time format is specified using
35             #pod L|POSIX/strftime>, portable code should restrict itself to
36             #pod formats using ANSI C89 specifiers.
37             #pod
38             #pod =head1 SEE ALSO
39             #pod
40             #pod L,
41             #pod which prefixes every line.
42             #pod
43             #pod =method TIEHANDLE
44             #pod
45             #pod Invoked by the command
46             #pod C.
47             #pod You may also specify a L|POSIX/strftime> string as an
48             #pod additional parameter to format the timestamp; by default the format is
49             #pod C<%x %X >, which is the local representation of the date and time
50             #pod followed by a space.
51             #pod
52             #pod =cut
53              
54             sub TIEHANDLE {
55 1     1   206 my ( $class, $handle_glob, $format ) = @_;
56 1   50     7 $format ||= '%x %X ';
57             return $class->SUPER::TIEHANDLE( $handle_glob,
58 1     2   12 sub { ( strftime( $format, localtime ), @_ ) } );
  2         6  
59             }
60              
61             1;
62              
63             __END__