File: bytes.pl

package info (click to toggle)
libmoose-autobox-perl 0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 292 kB
  • ctags: 241
  • sloc: perl: 2,402; makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,604 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl

use strict;
use warnings;

use Moose::Autobox;

{
    package # hide from PAUSE
        Units::Bytes;
    use Moose::Role;
    use Moose::Autobox;
    
    sub bytes     { $_[0]                   }    
    sub kilobytes { $_[0] * 1024            }
    sub megabytes { $_[0] * 1024->kilobytes }
    sub gigabytes { $_[0] * 1024->megabytes }
    sub terabytes { $_[0] * 1024->gigabytes }
    
    {
        no warnings 'once'; # << squelch the stupid "used only once, maybe typo" warnings
        *byte     = \&bytes;
        *kilobyte = \&kilobytes;    
        *megabyte = \&megabytes;    
        *gigabyte = \&gigabytes;
        *terabyte = \&terabytes;
    }
}

Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes');

$\ = "\n";

print "5 kilobytes are " . 5->kilobytes . " bytes";
print "2 megabytes are " . 2->megabytes . " bytes";
print "1 gigabyte is "   . 1->gigabyte  . " bytes";
print "2 terabyes are "  . 2->terabytes . " bytes";

=pod

=head1 NAME

Unit::Bytes

=head1 SYNOPSIS

  Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes');

  print "5 kilobytes are " . 5->kilobytes . " bytes";
  print "2 megabytes are " . 2->megabytes . " bytes";
  print "1 gigabyte is "   . 1->gigabyte  . " bytes";
  print "2 terabyes are "  . 2->terabytes . " bytes";

=head1 DESCRIPTION

This is a Moose::Autobox port of the perl6 vmethods example.

=head1 AUTHOR

Stevan Little, E<lt>stevan@iinteractive.comE<gt>

=head1 ACKNOLEDGEMENTS

This code was ported from the version in the Pugs 
examples/vmethods/ directory. See that for original author 
information.

=cut