Time-Duration-Object

 view release on metacpan or  search on metacpan

lib/Time/Duration/Object.pm  view on Meta::CPAN

use strict;
use warnings;
package Time::Duration::Object 0.302;
# ABSTRACT: Time::Duration, but an object

use Time::Duration 1.02;

#pod =head1 SYNOPSIS
#pod
#pod  use Time::Duration::Object;
#pod
#pod  my $duration = Time::Duration::Object->new($end_time - $start_time);
#pod
#pod =head1 DESCRIPTION
#pod
#pod This module provides an object-oriented interface to Time::Duration.  Sure,
#pod it's overkill, and Time::Duration is plenty useful without OO, but this
#pod interface makes it easy to use Time::Duration with Class::DBI, and that's a
#pod good thing.
#pod
#pod =head1 METHODS
#pod
#pod =head2 C< new($seconds) >
#pod
#pod This returns a new Time::Duration::Object for the given number of seconds.
#pod
#pod =cut

sub new {
	my ($class, $duration) = @_;
	return unless defined $duration;
	bless \$duration => $class;
}

#pod =head2 C< seconds >
#pod
#pod This returns the number of seconds in the duration (i.e., the argument you
#pod passed to your call to C<new>.)
#pod
#pod =cut

sub seconds {
	return ${(shift)};
}

#pod =head2 C<duration>
#pod
#pod =head2 C<duration_exact>
#pod
#pod =head2 C<ago>
#pod
#pod =head2 C<ago_exact>
#pod
#pod =head2 C<from_now>
#pod
#pod =head2 C<from_now_exact>
#pod
#pod =head2 C<later>
#pod
#pod =head2 C<later_exact>
#pod
#pod =head2 C<earlier>
#pod
#pod =head2 C<earlier_exact>
#pod
#pod These methods all perform the function of the same name from Time::Duration.
#pod
#pod =cut

{
  ## no critic (ProhibitNoStrict ProhibitNoWarnings)
  no strict 'refs';
  no warnings 'redefine';
  my @methods = map { $_, "$_\_exact" } qw(duration ago from_now later earlier);
  for (@methods) {
    my $method = \&{"Time::Duration::$_"};
    *{$_} = sub {
      unshift @_, ${(shift)};
      my $result = &$method(@_);
      bless \$result => 'Time::Duration::_Result';
    }
  }
}

package Time::Duration::_Result 0.302;

#pod =head2 as_string
#pod
#pod Time::Duration::Object methods don't return strings, they return an object that
#pod stringifies.  If you can't deal with that and don't want to stringify by
#pod concatenating an empty string, you can call C<as_string> instead.
#pod

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.275 second using v1.00-cache-2.02-grep-82fe00e-cpan-d29e8ade9f55 )