File Coverage

blib/lib/Tie/Scalar/Timestamp.pm
Criterion Covered Total %
statement 27 27 100.0
branch 4 6 66.6
condition 2 5 40.0
subroutine 9 9 100.0
pod n/a
total 42 47 89.3


line stmt bran cond sub pod time code
1             package Tie::Scalar::Timestamp;
2              
3 1     1   41116 use strict;
  1         2  
  1         41  
4 1     1   5 use warnings;
  1         2  
  1         32  
5              
6 1     1   5 use base qw(Tie::Scalar);
  1         6  
  1         1055  
7 1     1   822 use vars qw($VERSION $DEFAULT_STRFTIME);
  1         1  
  1         68  
8 1     1   5 use Carp;
  1         2  
  1         53  
9 1     1   1022 use POSIX qw(strftime);
  1         9681  
  1         7  
10              
11             $VERSION = '0.01';
12              
13             $DEFAULT_STRFTIME = '%Y-%m-%dT%H:%M:%S';
14              
15             sub TIESCALAR {
16 1     1   14 my $class = shift;
17 1   50     9 my $options = shift || {};
18 1         6 return bless $options, $class;
19             }
20              
21             sub STORE {
22 2     2   1504 my $self = shift;
23             # die unless asked not to (by having no_die in the tie statement)
24 2 100       2262 croak "Can't store to a Tie::Scalar::Timestamp variable" unless $self->{no_die};
25 1 50       7 carp "Can't store to a Tie::Scalar::Timestamp variable" if $^W;
26             }
27              
28             sub FETCH {
29 2     2   1001051 my $self = shift;
30 2   33     22 my $pattern = $self->{strftime} || $DEFAULT_STRFTIME;
31 2 50       431 strftime $pattern, ($self->{utc} ? gmtime : localtime );
32             }
33              
34             # module return
35             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 simple class that creates readonly scalars
63             that always return the current timestamp. By default, it uses
64             the format C (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 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.
75              
76             =over
77              
78             =item C
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
86              
87             Use UTC time instead of local time.
88              
89             =item C
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, L, L
100              
101             =head1 AUTHOR
102              
103             Peter Eichman, C<< >>
104              
105             =head1 COPYRIGHT AND LICENSE
106              
107             Copyright E2005 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