Tie::Scalar::Timestamp - Create a scalar that always returns the current timestamp


Tie-Scalar-Timestamp documentation Contained in the Tie-Scalar-Timestamp distribution.

Index


Code Index:

NAME

Top

Tie::Scalar::Timestamp - Create a scalar that always returns the current timestamp

SYNOPSIS

Top

    # 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';

DESCRIPTION

Top

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.

A variables tied to this class is readonly, and attempting to assign to it will raise an exception.

OPTIONS

Top

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.

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.

SEE ALSO

Top

perltie, Tie::Scalar, strftime(3)

AUTHOR

Top

Peter Eichman, <peichman@cpan.org>

COPYRIGHT AND LICENSE

Top


Tie-Scalar-Timestamp documentation Contained in the Tie-Scalar-Timestamp distribution.

package Tie::Scalar::Timestamp;

use strict;
use warnings;

use base qw(Tie::Scalar);
use vars qw($VERSION $DEFAULT_STRFTIME);
use Carp;
use POSIX qw(strftime);

$VERSION = '0.01';

$DEFAULT_STRFTIME = '%Y-%m-%dT%H:%M:%S';

sub TIESCALAR {
    my $class = shift;
    my $options = shift || {};
    return bless $options, $class;
}

sub STORE {
    my $self = shift;
    # die unless asked not to (by having no_die in the tie statement)
    croak "Can't store to a Tie::Scalar::Timestamp variable" unless $self->{no_die};
    carp "Can't store to a Tie::Scalar::Timestamp variable" if $^W;
}

sub FETCH {
    my $self = shift;
    my $pattern = $self->{strftime} || $DEFAULT_STRFTIME;
    strftime $pattern, ($self->{utc} ? gmtime : localtime );
}

# module return
1;