| 1 | package Tie::Handle::Filter::Output::Timestamp; | ||||||||||||
| 2 | |||||||||||||
| 3 | # ABSTRACT: prepend filehandle output with a timestamp | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use 5.008; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use base 'Tie::Handle::Filter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use POSIX 'strftime'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | 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<tie|perlfunc/tie> function to | ||||||||||||
| 26 | #pod prepend all output with a timestamp, optionally formatted according to | ||||||||||||
| 27 | #pod the L<POSIX C<strftime>|POSIX/strftime> function. Only the beginning of | ||||||||||||
| 28 | #pod strings given to L<C<print>|perlfunc/print>, | ||||||||||||
| 29 | #pod L<C<printf>|perlfunc/printf>, L<C<syswrite>|perlfunc/syswrite>, and | ||||||||||||
| 30 | #pod L<C<say>|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<C<strftime>|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<Tie::Handle::Filter::Output::Timestamp::EveryLine|Tie::Handle::Filter::Output::Timestamp::EveryLine>, | ||||||||||||
| 41 | #pod which prefixes every line. | ||||||||||||
| 42 | #pod | ||||||||||||
| 43 | #pod =method TIEHANDLE | ||||||||||||
| 44 | #pod | ||||||||||||
| 45 | #pod Invoked by the command | ||||||||||||
| 46 | #pod C<tie *glob, 'Tie::Handle::Filter::Output::Timestamp', *glob>. | ||||||||||||
| 47 | #pod You may also specify a L<C<strftime>|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 | 1 | ▶ | sub TIEHANDLE { | ||||||||||
|
Subroutine
TIEHANDLE: called CC=2 SCAR=7.0
| |||||||||||||
| 55 | 1 | my ( $class, $handle_glob, $format ) = @_; | |||||||||||
| 56 | 1 | ▶ | $format ||= '%x %X '; | ||||||||||
|
Condition50%1/2
$format ||= "%x %X "
MC/DC0%0/1
$format ||= "%x %X "
$format
Truth table50%1/2
A
A= $format
| |||||||||||||
| 57 | return $class->SUPER::TIEHANDLE( $handle_glob, | ||||||||||||
| 58 | 1 | ▶ | sub { ( strftime( $format, localtime ), @_ ) } ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 2 | 1; | |||||||||||
| 62 | |||||||||||||
| 63 | __END__ | ||||||||||||