=head1 NAME MooseX::Types::NumUnit - Type(s) for using units in Moose =head1 SYNOPSIS package MyPackage use Moose; use MooseX::Types::NumUnit qw/NumUnit NumSI num_of_unit/; has 'quantity' => ( isa => NumUnit, default => 0 ); has 'si_quantity' => ( isa => NumSI, required => 1 ); has 'length' => ( isa => num_of_unit('m'), default => '1 ft' ); =head1 DESCRIPTION This module provides types (C and friends) for Moose which represent physical units. More accurately it provides String to Number coercions, so that even if the user inputs a number with an incorrect (but compatible) unit, it will automatically coerce to a number of the correct unit. A few things to note: since C and friends are subtypes of C, a purely numerical value will not be coerced. This is by design, but should be kept in mind. Also C and friends are coerced by default (see L). =head1 PACKAGE VARIABLES =head2 C<$MooseX::Types::NumUnit::Verbose> When set to a true value, a string representing any conversion will be printed to C during coercion. =head1 TYPE-LIKE FUNCTIONS Since version 0.02, C does not provide global types. Rather it has exportable type-like function which behave like types but do not pollute the "type namespace". While they behave like types, remember they are functions and they should not be quoted when called. They are null prototyped though, should they shouldn't (usually) need parenthesis. Futher they are not exported by default and must be requested. For more information about this system see L. =head2 C A subtype of C which accepts a number with a unit, but discards the unit on coercion to a C. This is the parent unit for all other units provided herein. Of course those have different coercions. =head2 C A subtype of C which coerces to the SI equivalent of the unit passed in (i.e. a number in feet will be converted to a number in meters). In truth it is not strictly the SI equivalent, but whatever L thinks is the base unit. This should always be SI (I hope!). =head1 ANONYMOUS TYPE GENERATORS This module provides functions which return anonymous types which satisfy certain criteria. These functions may be exported on request, but are not exported by default. =head2 C Creates an anonymous type which has the given C<$unit>. If a number is passed in which can be converted to the specified unit, it is converted on coercion. If the number cannot be converted, the value of the attribute is set to C<0> and a warning is thrown. =head2 C Creates an anonymous type which has the SI equivalent of the given C<$unit>. This is especially handy for composite units when you don't want to work out by hand what the SI base would be. As a simple example, if C<$unit> is C<'ft'>, numbers passed in will be converted to meters! You see, the unit only helps specify the type of unit, however the SI unit is used. Another way to think of these types is as a resticted C of a certian quantity, allowing a loose specification. As with C, if a number is passed in which can be converted to the specified (SI) unit, it is converted on coercion. If the number cannot be converted, the value of the attribute is set to C<0> and a warning is thrown. =head1 AUTOMATIC COERCION Since the NumUnit types provided by this module are essentially just C types with special coercions, it doesn't make sense to use them without coercions enabled on the attribute. To that end, this module mimics L, with the exception that it only enables coercion on C and its subtypes. To prevent this, manually set C<< coerce => 0 >> for a given attribute and it will be left alone, or better yet, just use C as the type. =head1 NOTES This module defines the unit C (C) which L inexplicably lacks. Also the author is investigating if C can be changed to C from C, but so far this has not happened. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =back =head1 SOURCE REPOSITORY L =head1 AUTHOR Joel Berger, Ejoel.a.berger@gmail.comE =head1 COPYRIGHT AND LICENSE Copyright (C) 2012 by Joel Berger This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.