The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Petrophysics::Units - Perl extension for a "database" of units of measurement, and methods to search/lookup units and do conversion.


 use Petrophysics::Units;
 # Lookup unit by unique id
 my $m = Petrophysics::Units->lookup ('m');
 # Or by annotation.
 # There is no guarantee that the annotation is unique.
 # So treat with care..
 my $ft = Petrophysics::Units->lookup_annotation ('ft');
 # Are the two units compatible?
 printf "They are%s compatible\n", $m->is_compatible ($ft) ? '' : ' not';
 # Convert 1000 meters to feet
 my $number = $m->scalar_convert ($ft, 1000.0);
 # Convert an array of values from meters to feet
 my $numbers = $m->vector_convert (
        $ft, [ 1000.0, 2000.0, 3000.0 ]
 for my $u ($m, $ft) {
   printf "Unit unique id = '%s' is%s a base unit\n",
        $u->id, $u->is_base_unit ? '' : ' not';
   printf "Unit long name = '%s'\n", $u->name;
   printf "Unit short name (annotation) = '%s'\n", $u->annotation;
   printf "Unit quantity type = '%s'\n",
        ($u->quantity_type || 'unknown');
   printf "Unit display = '%s'\n", ($u->display || 'unknown');
   printf "Unit catalog name = '%s'\n", $u->catalog_name;
   printf "Unit catalog symbol = '%s'\n", $u->catalog_symbol;
   if ($u->is_base_unit) {
     printf "Unit description = '%s'\n", $u->description;
   } else {
     printf "Unit base unit = '%s'\n", $u->base_unit->id;
     print "Unit conversion to base = (A + Bx)/(C + Dx)\n";
     printf "  A = %.6f, B = %.6f, C = %.6f, D = %.6f\n",
        $u->A, $u->B, $u->C, $u->D;
 # Generic search
 my @units = Petrophysics::Units->grep (
        sub { $_->annotation =~ /ft/ }
 # Return all defined units
 my @all_units = Petrophysics::Units->all_units;


A "database" of units of measurement, and methods to lookup/search this database and to convert numbers from one unit to another.


The following POSC (c) products were used in the creation of this work:

  • epsgUnits.xml

  • poscUnits.xml

These files can be downloaded from Please see

POSC's license does not allow redistribution of unchanged files, nor using POSC in the name of a derived product. POSC will not guarantee the accuracy of this data, nor will I.


The "database" is provided in a file ("") which "happens" to contain text directly parsable by perl. The original databases were written in xml.

The database is included by this module, all objects in the database "happen" to be blessed into the correct class, and the class then includes methods to lookup units from the database, to search for units in the database, and to convert numbers from one unit to another.

Please note: Only the id of a unit is guaranteed to be unique. The annotation is almost unique, and is probably unique enough for normal use (and also far more readable).

The utility to convert the original database (xml files) to perl is included (but not installed) for reference.


None by default.


The following modules can be found on CPAN. Please check if any of those could satisfy your unit conversion needs better than this module.

  • Math::Unit

  • Physics::Unit

This file is part of the "OSPetro" project. Please see for further details.


Bjarne Steinsbo, <<gt>


Copyright 2003 by Bjarne Steinsbo

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.