1Class::Measure(3pm) User Contributed Perl Documentation Class::Measure(3pm)
2
3
4
6 Class::Measure - Create, compare, and convert units of measurement.
7
9 See Class::Measure::Length for some examples.
10
12 This is a base class that is inherited by the Class::Measure classes.
13 This distribution comes with the class Class::Measure::Length.
14
15 The classes Class::Measure::Area, Class::Measure::Mass,
16 Class::Measure::Space, Class::Measure::Temperature, and
17 Class::Measure::Volume are planned and will be added soon.
18
19 The methods described here are available in all Class::Measure classes.
20
22 new
23 my $m = new Class::Measure::Length( 1, 'inch' );
24
25 Creates a new measurement object. You must pass an initial measurement
26 and default unit.
27
28 In most cases the measurement class that you are using will export a
29 method to create new measurements. For example Class::Measure::Length
30 exports the length() method.
31
32 unit
33 my $unit = $m->unit();
34
35 Returns the object's default unit.
36
37 set_unit
38 $m->set_unit( 'feet' );
39
40 Sets the default unit of the measurement.
41
42 value
43 my $yards = $m->value('yards');
44 my $val = $m->value();
45 print "$m is the same as $val when in a string\n";
46
47 Retrieves the value of the measurement in the default unit. You may
48 specify a unit in which case the value is converted to the unit and
49 returned.
50
51 This method is also used to handle overloading of stringifying the
52 object.
53
54 set_value
55 my $m = length( 0, 'inches' );
56 $m->set_value( 12 ); # 12 inches.
57 $m->set_value( 1, 'foot' ); # 1 foot.
58
59 Sets the measurement in the default unit. You may specify a new
60 default unit as well.
61
62 reg_units
63 Class::Measure::Length->reg_units(
64 'inch', 'foot', 'yard',
65 );
66
67 Registers one or more units for use in the specified class. Units
68 should be in the singular, most common, form.
69
70 units
71 my @units = Class::Measure::Length->units();
72
73 Returns a list of all registered units.
74
75 reg_aliases
76 Class::Measure::Length->reg_aliases(
77 ['feet','ft'] => 'foot',
78 ['in','inches'] => 'inch',
79 'yards' => 'yard'
80 );
81
82 Register alternate names for units. Expects two arguments per unit to
83 alias. The first argument being the alias (scalar) or aliases (array
84 ref), and the second argument being the unit to alias them to.
85
86 reg_convs
87 Class::Measure::Length->reg_convs(
88 12, 'inches' => 'foot',
89 'yard' => '3', 'feet'
90 );
91
92 Registers a unit conversion. There are three distinct ways to specify
93 a new conversion. Each requires three arguments.
94
95 $count1, $unit1 => $unit2
96 $unit1 => $count2, $unit2
97
98 These first two syntaxes create automatic reverse conversions as well.
99 So, saying there are 12 inches in a foot implies that there are 1/12
100 feet in an inch.
101
102 $unit1 => $unit2, $sub
103
104 The third syntax accepts a subroutine as the last argument the
105 subroutine will be called with the value of $unit1 and it's return
106 value will be assigned to $unit2. This third syntax does not create a
107 reverse conversion automatically.
108
110 Please submit bugs and feature requests to the Class-Measure GitHub
111 issue tracker:
112
113 <https://github.com/bluefeet/Class-Measure/issues>
114
116 Aran Clary Deltac <bluefeet@gmail.com>
117
119 Roland van Ipenburg <roland@rolandvanipenburg.com>
120
122 This library is free software; you can redistribute it and/or modify it
123 under the same terms as Perl itself.
124
125
126
127perl v5.36.1 2023-06-08 Class::Measure(3pm)