615976d7bcf57cd04d4df6a176203fdc0c972897
[gitmo/Moose-Autobox.git] / examples / units / bytes.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Moose::Autobox;
7
8 {
9     package # hide from PAUSE
10         Units::Bytes;
11     use Moose::Role;
12     use Moose::Autobox;
13     
14     sub bytes     { $_[0]                   }    
15     sub kilobytes { $_[0] * 1024            }
16     sub megabytes { $_[0] * 1024->kilobytes }
17     sub gigabytes { $_[0] * 1024->megabytes }
18     sub terabytes { $_[0] * 1024->gigabytes }
19     
20     {
21         no warnings 'once'; # << squelch the stupid "used only once, maybe typo" warnings
22         *byte     = \&bytes;
23         *kilobyte = \&kilobytes;    
24         *megabyte = \&megabytes;    
25         *gigabyte = \&gigabytes;
26         *terabyte = \&terabytes;
27     }
28 }
29
30 Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes');
31
32 $\ = "\n";
33
34 print "5 kilobytes are " . 5->kilobytes . " bytes";
35 print "2 megabytes are " . 2->megabytes . " bytes";
36 print "1 gigabyte is "   . 1->gigabyte  . " bytes";
37 print "2 terabyes are "  . 2->terabytes . " bytes";
38
39 =pod
40
41 =head1 NAME
42
43 Unit::Bytes
44
45 =head1 SYNOPSIS
46
47   Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes');
48
49   print "5 kilobytes are " . 5->kilobytes . " bytes";
50   print "2 megabytes are " . 2->megabytes . " bytes";
51   print "1 gigabyte is "   . 1->gigabyte  . " bytes";
52   print "2 terabyes are "  . 2->terabytes . " bytes";
53
54 =head1 DESCRIPTION
55
56 This is a Moose::Autobox port of the perl6 vmethods example.
57
58 =head1 AUTHOR
59
60 Stevan Little, E<lt>stevan@iinteractive.comE<gt>
61
62 =head1 ACKNOLEDGEMENTS
63
64 This code was ported from the version in the Pugs 
65 examples/vmethods/ directory. See that for original author 
66 information.
67
68 =cut