Tie::Scalar::Timestamp - Create a scalar that always returns the current timestamp
# create a timestamp variable that uses localtime # and yyyy-mm-ddThh:mm:ss (ISO8601) format tie my $timestamp, 'Tie::Scalar::Timestamp'; print "$timestamp\n"; # e.g. 2005-02-25T11:02:34 sleep 2; # wait 2 seconds... print "$timestamp\n"; # ... 2005-02-25T11:02:36 # this will die; $timestamp is a readonly variable $timestamp = '2004'; # create a timestamp variable that returns just the time in UTC tie my $utc_timestamp, 'Tie::Scalar::Timestamp', { strftime => '%H:%M:%S', utc => 1 }; # set the default format $Tie::Scalar::Timestamp::DEFAULT_STRFTIME = '%H:%M:%S';
This is a very simple class that creates readonly scalars that always return the current timestamp. By default, it uses the format yyyy-mm-ddThh:mm:ss (or, in strftime notation, %Y-%m-%dT%H:%M:%S) and local time. You can optionally pass a hashref of options to the call to tie to specify a pattern and whether to use UTC time instead of local time.
yyyy-mm-ddThh:mm:ss
%Y-%m-%dT%H:%M:%S
tie
A variables tied to this class is readonly, and attempting to assign to it will raise an exception.
The following options can be passed in a hashref to tie.
strftime
The strftime pattern to fromat the timestamp as. The default pattern is %Y-%m-%dT%H:%M:%S. To change the default, set $Tie::Scalar::Timestamp::DEFAULT_STRFTIME to your prefered pattern.
$Tie::Scalar::Timestamp::DEFAULT_STRFTIME
utc
Use UTC time instead of local time.
no_die
Do not throw an exception when attempting to assign to a timestamp. This module will still emit a warning if you have warnings enabled.
perltie, Tie::Scalar, strftime(3)
Peter Eichman, <peichman@cpan.org>
<peichman@cpan.org>
Copyright ©2005 by Peter Eichman.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Tie::Scalar::Timestamp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Scalar::Timestamp
CPAN shell
perl -MCPAN -e shell install Tie::Scalar::Timestamp
For more information on module installation, please visit the detailed CPAN module installation guide.