| 1 | package Tie::Scalar::Timestamp; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use base qw(Tie::Scalar); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use vars qw($VERSION $DEFAULT_STRFTIME); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | $VERSION = '0.01'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | $DEFAULT_STRFTIME = '%Y-%m-%dT%H:%M:%S'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | ▶ | sub TIESCALAR { | ||||||||||||||||||
|
Subroutine
TIESCALAR: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 16 | 1 | my $class = shift; | |||||||||||||||||||
| 17 | 1 | ▶ | my $options = shift || {}; | ||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 18 | 1 | return bless $options, $class; | |||||||||||||||||||
| 19 | } | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 2 | ▶ | sub STORE { | ||||||||||||||||||
|
Subroutine
STORE: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 22 | 2 | my $self = shift; | |||||||||||||||||||
| 23 | # die unless asked not to (by having no_die in the tie statement) | ||||||||||||||||||||
| 24 | 2 | ▶ | croak "Can't store to a Tie::Scalar::Timestamp variable" unless $self->{no_die}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 1 | ▶ | carp "Can't store to a Tie::Scalar::Timestamp variable" if $^W; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2 | ▶ | sub FETCH { | ||||||||||||||||||
|
Subroutine
FETCH: called CC=3 SCAR=12.5
| |||||||||||||||||||||
| 29 | 2 | my $self = shift; | |||||||||||||||||||
| 30 | 2 | ▶ | my $pattern = $self->{strftime} || $DEFAULT_STRFTIME; | ||||||||||||||||||
|
Condition33%1/3
$self->{'strftime'} || $Tie::Scalar::Timestamp::DEFAULT_STRFTIME
MC/DC0%0/2
$self->{'strftime'} || $Tie::Scalar::Timestamp::DEFAULT_STRFTIME
$self->{'strftime'}
$Tie::Scalar::Timestamp::DEFAULT_STRFTIME
Truth table33%1/3
A || B
A= $self->{'strftime'}B= $Tie::Scalar::Timestamp::DEFAULT_STRFTIME
| |||||||||||||||||||||
| 31 | 2 | ▶ | strftime $pattern, ($self->{utc} ? gmtime : localtime ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | # module return | ||||||||||||||||||||
| 35 | 1 | 1; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =head1 NAME | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | Tie::Scalar::Timestamp - Create a scalar that always returns the current timestamp | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 SYNOPSIS | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # create a timestamp variable that uses localtime | ||||||||||||||||||||
| 44 | # and yyyy-mm-ddThh:mm:ss (ISO8601) format | ||||||||||||||||||||
| 45 | tie my $timestamp, 'Tie::Scalar::Timestamp'; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | print "$timestamp\n"; # e.g. 2005-02-25T11:02:34 | ||||||||||||||||||||
| 48 | sleep 2; # wait 2 seconds... | ||||||||||||||||||||
| 49 | print "$timestamp\n"; # ... 2005-02-25T11:02:36 | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # this will die; $timestamp is a readonly variable | ||||||||||||||||||||
| 52 | $timestamp = '2004'; | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # create a timestamp variable that returns just the time in UTC | ||||||||||||||||||||
| 55 | tie my $utc_timestamp, 'Tie::Scalar::Timestamp', { strftime => '%H:%M:%S', utc => 1 }; | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # set the default format | ||||||||||||||||||||
| 58 | $Tie::Scalar::Timestamp::DEFAULT_STRFTIME = '%H:%M:%S'; | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =head1 DESCRIPTION | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | This is a B<very> simple class that creates readonly scalars | ||||||||||||||||||||
| 63 | that always return the current timestamp. By default, it uses | ||||||||||||||||||||
| 64 | the format C<yyyy-mm-ddThh:mm:ss> (or, in strftime notation, | ||||||||||||||||||||
| 65 | C<%Y-%m-%dT%H:%M:%S>) and local time. You can optionally pass | ||||||||||||||||||||
| 66 | a hashref of options to the call to C<tie> to specify a pattern | ||||||||||||||||||||
| 67 | and whether to use UTC time instead of local time. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | A variables tied to this class is readonly, and attempting to | ||||||||||||||||||||
| 70 | assign to it will raise an exception. | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head1 OPTIONS | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | The following options can be passed in a hashref to C<tie>. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =over | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =item C<strftime> | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | The strftime pattern to fromat the timestamp as. The default | ||||||||||||||||||||
| 81 | pattern is C<%Y-%m-%dT%H:%M:%S>. To change the default, set | ||||||||||||||||||||
| 82 | C<$Tie::Scalar::Timestamp::DEFAULT_STRFTIME> to your prefered | ||||||||||||||||||||
| 83 | pattern. | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =item C<utc> | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | Use UTC time instead of local time. | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =item C<no_die> | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Do not throw an exception when attempting to assign to a | ||||||||||||||||||||
| 92 | timestamp. This module will still emit a warning if you | ||||||||||||||||||||
| 93 | have warnings enabled. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =back | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =head1 SEE ALSO | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | L<perltie>, L<Tie::Scalar>, L<strftime(3)> | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =head1 AUTHOR | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | Peter Eichman, C<< <peichman@cpan.org> >> | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | Copyright E<copy>2005 by Peter Eichman. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | This program is free software; you can redistribute it | ||||||||||||||||||||
| 110 | and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||