0.03 release
[gitmo/Moose-Autobox.git] / examples / units / time.pl
CommitLineData
be334002 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Moose::Autobox;
be334002 7
8{
68a5f1ac 9 package # hide me from PAUSE
10 Units::Time;
be334002 11 use Moose::Role;
12 use Moose::Autobox;
13
14 sub seconds { $_[0] }
15 sub minutes { $_[0] * 60 }
16 sub hours { $_[0] * 60->minutes }
17 sub days { $_[0] * 24->hours }
18 sub weeks { $_[0] * 7->days }
19 sub years { $_[0] * 365->days }
20 sub centuries { $_[0] * 10->years }
21
22 sub ago {
23 my ($self, $time) = @_;
24 $time ||= time();
25 $time - $self;
26 }
27
28 sub since {
29 my ($self, $time) = @_;
30 $time ||= time();
31 $time + $self;
32 }
33
34 {
35 no warnings 'once';
36
37 # singular versions
38 *second = \&seconds;
39 *minute = \&minutes;
40 *hour = \&hours;
41 *day = \&days;
42 *week = \&weeks;
43 *year = \&years;
44 *century = \&centuries;
45
46 *til = \&ago;
47 *from_now = \&since;
48 }
49
50 sub as_string { scalar localtime $_[0] }
51
52}
53
244bd352 54Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Time');
be334002 55
56$\ = "\n";
57
58print "2 days ago was : " . 2->days->ago->as_string;
59print "3 weeks from now will be : " . 3->weeks->from_now->as_string;
60my $one_week_ago = 1->week->ago;
61print "1 day until 1 week ago : " . 1->day->til($one_week_ago)->as_string;
62print "2 years since 1 week ago : " . 2->years->since($one_week_ago)->as_string;
63
68a5f1ac 64=pod
be334002 65
68a5f1ac 66=head1 NAME
67
68Unit::Time
69
70=head1 SYNOPSIS
71
72 Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Time');
73
74 print "2 days ago was : " . 2->days->ago->as_string;
75 print "3 weeks from now will be : " . 3->weeks->from_now->as_string;
76 print "1 day until 1 week ago : " . 1->day->til(1->week->ago)->as_string;
77 print "2 years since 1 week ago : " . 2->years->since(1->week->ago)->as_string;
78
79=head1 DESCRIPTION
80
81This is a Moose::Autobox port of the perl6 vmethods example.
82
83=head1 AUTHOR
84
85Stevan Little, E<lt>stevan@iinteractive.comE<gt>
86
87=head1 ACKNOLEDGEMENTS
88
89This code was ported from the version in the Pugs
90examples/vmethods/ directory. See that for original author
91information.
92
93=cut