UNITS(1)
π¦ units(1) β the conversion program, bestie β¨
Okay so real talk: this is units, the conversion program, and it lives in your Perl tool kit (user contributed perl documentation, iykyk). She converts quantities from one scale to another and she does it flawlessly. Let's get into it π
πΈ NAME πΈ
units β conversion program. That's the whole vibe. Converting things. Iconic.
π SYNOPSIS π
So you can just run her with no args and she'll chat with you:
% units
You have: in
You want: cm
* 2.54
/ 0.393701
Or you can be all business and pass stuff on the command line:
% units [-f /path/to/unittab] [want_unit have_unit]
β¨ OPTIONS β¨
-fβ use the definition file you specify, like the main character you are π--versionβ display version information, that's it, that's the flag π«Ά
π₯ DESCRIPTION π₯
NOTE: this does not handle the Gnu units format (https://www.gnu.org/software/units/). Just so we're clear, bestie π
The units program converts quantities expressed in various scales to their equivalents in other scales. She can only handle multiplicative or affine scale changes β the one exception being temperature. Okay?
She works in one of two ways:
- If you give her two units as command line arguments, she reports the conversion. Done. Periodt.
- Otherwise she operates interactively, prompting you for inputs like the sweet little REPL she is:
% units
You have: meters
You want: feet
* 3.2808399
/ 0.3048
You have: cm3
You want: gallons
* 0.00026417205
/ 3785.4118
You have: meters/s
You want: furlongs/fortnight
* 6012.8848
/ 0.00016630952
You have: 1|2 inch
You want: cm
* 1.27
/ 0.78740157
You have: 98.6 F
You want: C
98.6 F is 37 C
You have: -40 C
You want: F
-40 C is -40 F
π Powers, multiplication, division β the grammar lesson
Powers of units can be specified using the '^' character as shown in the example, or by simple concatenation: 'cm3' is equivalent to 'cm^3'. Slay, so convenient.
Multiplication of units can be specified by using spaces, a dash or an asterisk. Three whole ways. Choose your fighter.
Division of units is indicated by the slash ('/'). Heads up though: multiplication has a higher precedence than division, so 'm/s/s' is the same as 'm/s^2' or 'm/s s'. Not a typo, that's just how she rolls.
Division of numbers must be indicated using the vertical bar ('|'). So to convert half a meter, you would write '1|2 meter'. If you write '1/2 meter' then the units program would interpret that as equivalent to '0.5/meter'. π Yeah. Different thing entirely. Don't do that to yourself.
π When units don't match (conformability error)
If you enter incompatible unit types, the units program will print a message indicating that the units are not conformable and it will display the reduced form for each unit:
You have: ergs/hour
You want: fathoms kg^2 / day
conformability error
2.7777778e-11 kg m^2 / sec^3
2.1166667e-05 kg^2 m / sec
Not me getting a whole error essay π but honestly it's helpful.
π§ The data file & the constants
The conversion information is read from a units data file. The default file includes definitions for most familiar units, abbreviations and metric prefixes. Some constants of nature included are:
pi ratio of circumference to diameter
c speed of light
e charge on an electron
g acceleration of gravity
force same as g
mole Avogadro's number
water pressure per unit height of water
mercury pressure per unit height of mercury
au astronomical unit
The unit 'pound' is a unit of mass. Compound names are run together so 'pound force' is a unit of force. The unit 'ounce' is also a unit of mass. The fluid ounce is 'floz'. British units that differ from their US counterparts are prefixed with 'br', and currency is prefixed with its country name: 'belgiumfranc', 'britainpound'.
When searching for a unit, if the specified string does not appear exactly as a unit name, then units will try to remove a trailing 's' or a trailing 'es' and check again for a match. She's doing her best to meet you where you're at π«Ά
π Making your own units file
To find out what units are available read the standard units file. If you want to add your own units you can supply your own file. If no standard file exists and you do not supply your own file, this program uses internal data. Built-in fallback, we love her.
A unit is specified on a single line by giving its name and an equivalence. Be careful to define new units in terms of old ones so that reductions leads to primitive units.
Primitive (a.k.a. fundamental) units are defined with a string of three characters which begin and end with '*' or '!'.
Real warning, bestie: note that the units program will not detect infinite loops that could be caused by careless unit definitions. Like, she will just spin forever. Be normal about it.
π Comments
Comments in the unit definition file begin with a '/' or '#' character at the beginning of a line. Once the parser has successfully parsed a unit name and it's definition, the remainder of the line is ignored. This makes it safe to include in-line comments. Casual. Effortless.
π Prefixes
Prefixes are defined in the same way as standard units, but with a trailing dash at the end of the prefix name. If a unit is not found even after removing trailing 's' or 'es', then it will be checked against the list of prefixes. Prefixes will be removed until a legal base unit is identified. She keeps peeling until she finds the one π
β Example units file
Here is an example of a short units file that defines some basic units.
m !a!
sec ***
Temperature ***
micro- 1e-6
minute 60 sec
hour 60 min
inch 0.0254 m
ft 12 inches
mile 5280 ft
π§ Temperature, the special one
If a "Temperature" dimension is defined in the units table, then you can define various temperature scales as units by specifying the code needed to convert the unit to or from Kelvin. The built-in units table has definitions for Kelvin (K), Celsius (C), Fahrenheit (F) and Rankine (R).
The code consists of a perl hash containing the keys 'to' and 'from'. The values are the subroutine definitions necessary to convert a value from Kelvin to the specified unit, or to Kelvin from the the specified unit. See the built-in unit table for examples. (Yes, "the the". She said it. We keep it. π
)
A temperature unit entered at "You have" without any constant preceding it will default to zero units. This is in contrast to non-temperature units, where a bare unit name is assumed to mean 1 unit. Also, for temperatures only, negative constants are allowed. This enables, for example, a conversation between -40C and F. Which is also just, like, a fun fact about physics? Obsessed.
π AUTHOR π
Mark-Jason Dominus, "<mjd-perl-units@plover.com>"
Temperature support by Gary Puckering, "<jgpuckering@rogers.com>"
Currently maintained in https://github.com/briandfoy/PerlPowerTools
π BUGS π
Literally nothing here. Empty section. A peaceful, silent BUGS section. We stan a bug-free queen (or at least a queen who isn't listing them) β¨
π COPYRIGHT and LICENSE π
This program is copyright (c) M-J. Dominus (1996, 1999).
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version, or under Perl's 'Artistic License'.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
No cap on that warranty bit β it's in all caps for a reason, so read it, don't skip it π
πΈ okay that's the whole page, you're a units expert now, go convert something unhinged like furlongs per fortnight πΈ
This little beauty is perl v5.42.2, dated 2026-02-15, and you can find her under UNITS(1) in the manual. Mwah π
π¦ where this page comes from
- alpine 3.24: perl-power-tools-doc 1.053-r1 Β· GPL-2.0-or-later AND Artistic-1.0-Perl AND Artistic-2.0 AND BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms