| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | Templer::Timer - A utility class for recording build-time. | ||
| 5 | |||
| 6 | =cut | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use Templer::Timer; | ||
| 14 | |||
| 15 | my $obj = Templer::Timer->new(); | ||
| 16 | sleep( 3 ); | ||
| 17 | |||
| 18 | print $obj->elapsed(); | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This class is a simple utility for reporting the time elapsed since | ||
| 25 | the object was created. | ||
| 26 | |||
| 27 | It is used to report on the build-time of a templer site. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 LICENSE | ||
| 32 | |||
| 33 | This module is free software; you can redistribute it and/or modify it | ||
| 34 | under the terms of either: | ||
| 35 | |||
| 36 | a) the GNU General Public License as published by the Free Software | ||
| 37 | Foundation; either version 2, or (at your option) any later version, | ||
| 38 | or | ||
| 39 | |||
| 40 | b) the Perl "Artistic License". | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | =head1 AUTHOR | ||
| 45 | |||
| 46 | Steve Kemp <steve@steve.org.uk> | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | =head1 COPYRIGHT AND LICENSE | ||
| 51 | |||
| 52 | Copyright (C) 2015 Steve Kemp <steve@steve.org.uk>. | ||
| 53 | |||
| 54 | This library is free software. You can modify and or distribute it under | ||
| 55 | the same terms as Perl itself. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | |||
| 60 | use strict; | ||
| 61 | use warnings; | ||
| 62 | |||
| 63 | |||
| 64 | package Templer::Timer; | ||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | =begin doc | ||
| 69 | |||
| 70 | Constructor. | ||
| 71 | |||
| 72 | Record the creation time of this object. | ||
| 73 | |||
| 74 | =end doc | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub new | ||
| 79 | { | ||
| 80 | my ( $proto, %supplied ) = (@_); | ||
| 81 | my $class = ref($proto) || $proto; | ||
| 82 | |||
| 83 | my $self = {}; | ||
| 84 | $self->{ 'start' } = time(); | ||
| 85 | |||
| 86 | bless( $self, $class ); | ||
| 87 | return $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | =begin doc | ||
| 93 | |||
| 94 | Return the time elapsed since this object was created. | ||
| 95 | |||
| 96 | =end doc | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub elapsed | ||
| 101 | { | ||
| 102 | my ($self) = (@_); | ||
| 103 | |||
| 104 | my $now = time; | ||
| 105 | my $elapsed = $now - $self->{ 'start' }; | ||
| 106 | |||
| 107 | if ( $elapsed < 1 ) | ||
| 108 | { | ||
| 109 | $elapsed = "less than 1 second"; | ||
| 110 | } | ||
| 111 | elsif ( $elapsed == 1 ) | ||
| 112 | { | ||
| 113 | $elapsed = "1 second"; | ||
| 114 | } | ||
| 115 | else | ||
| 116 | { | ||
| 117 | $elapsed = $elapsed . " seconds"; | ||
| 118 | } | ||
| 119 | $elapsed; | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; |