# Distances $d = new Physics::Unit::Distance('98 mi'); print $d->ToString, "\n"; # prints 157715.712 meter $d->add('10 km'); print $d->ToString, "\n"; # prints 167715.712 meter print $d->value, ' ', $d->default_unit->name, "\n"; # same thing # Convert print $d->ToString('mile'), "\n"; # prints 104.213... mile print $d->convert('mile'), " miles\n"; # same thing (except 'miles') $d2 = new Physics::Unit::Distance('2000'); # no unit given, use the default print $d2->ToString, "\n"; # prints 2000 meter # Times $t = Physics::Unit::Time->new('36 hours'); print $t->ToString, "\n"; # prints 129600 second # Speed = Distance / Time $s = $d->div($t); # $s is a Physics::Unit::Speed object print $s->ToString, "\n"; # prints 1.2941... mps # Automatic typing $s = new Physics::Unit::Scalar('kg m s'); # Unrecognized type print ref $s, "\n"; # $s is a Physics::Unit::Scalar $f = $s->div('3000 s^3'); print ref $f, "\n"; # $f is a Physics::Unit::Force
This package encapsulates information about physical quantities. Each instance of a class that derives from Physics::Unit::Scalar holds the value of some type of measurable quantity. When you use this module, several new classes are immediately available. See the UnitsByType page for a list of types included with the unit library.
You will probably only need to use the classes that derive from Physics::Unit::Scalar, such as Physics::Unit::Distance, Physics::Unit::Speed, etc. You can also define your own derived classes, based on types of physical quantities.
This module relies heavily on Physics::Unit. Each Scalar object references a Unit object that defines the dimensionality of the Scalar. The dimensionality also identifies (usually) the type of physical quantity that is stored, and the derived class of the object.
For example, the class Physics::Unit::Distance uses the Physics::Unit object named 'meter' to define the scale of its object instances. The type of the 'meter' object is 'Distance'.
Defining classes that correspond to physical quantity types allows us to overload the arithmetic methods to produce derived classes of the correct type automatically. For example:
$d = new Physics::Unit::Distance('98 mi'); $t = new Physics::Unit::Time('36 years'); # $s will be of type Physics::Unit::Speed. $s = $d->div($t);
When a new object is created, this package attempts to determine its subclass based on its dimensionality. Thus, when you multiply two Distances together, the result is an Area object. This behavior can be selectively overridden when necessary. For example, both energy and torque have the same dimensions, Force * Distance. Therefore, it remains the programmer's responsibility, in this case, to assign the correct subclass to Scalars that have this dimensionality.
See also the Physics::Unit::Scalar::Implementation page for more details.
By default, this module exports nothing. You can request all of the functions to be exported as follows:
use Physics::Unit::Scalar ':ALL';
Or, you can just get specific ones. For example:
use Physics::Unit::Scalar qw( ScalarFactory GetScalar );
The class of the resultant object matches the type of the unit created. If the type is not recognized or ambiguous, then the class of the resultant object will be Physics::Unit::Scalar.
This convenience function takes an object reference or an expression, and returns a Physics::Unit::Scalar object. If
$argis an object reference, it is simply returned.
Package or object method. This makes a new user defined Scalar (or derived class) object. For example:
# This creates an object of a derived class $d = new Physics::Unit::Distance('3 miles'); # This does the same thing; the type is figured out automatically # $d will be a Physics::Unit::Distance $d = new Physics::Unit::Scalar('3 miles'); # Use the default unit for the subtype (for Distance, it's meters): $d = new Physics::Unit::Distance(10); # This defaults to one meter: $d = new Physics::Unit::Distance; # Copy constructor: $d2 = $d->new;
If the type cannot be identified by the dimensionality of the unit, then a Physics::Unit::Scalar object is returned.
Returns a string representation of the scalar, either in the default units or the unit specified.
Get the default unit object which is used when printing out the given Scalar.
Returns the numerical value of this scalar expressed in the given Unit.
Get or set the value.
Add another Scalar to the provided one.
Take the negative of the Scalar
Subtract another Scalar from this one.
This returns a new object which is the product of
$selfand the argument. Neither the original object nor the argument is changed.
[FIXME: see Github issue #22. These methods should all work the same way. Either all change the value of the existing object, or all return a new object.]
Returns a new Scalar object which is the reciprocal of the object. The original object is unchanged.
This returns a new Scalar object which is a quotient. Neither the original object nor the argument is changed.
Chris Maloney <firstname.lastname@example.org>
COPYRIGHT AND LICENSE
Copyright 2002-2003 by Chris Maloney
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.