#! /usr/bin/perl
use strict;
use warnings;
print <<EOF;
units calculator by stefan\@blinkenarea.org
enter expressions to calculate
supported suffixes on numbers:
mm millimeters
mil 1/1000 inch
pcb PCB file units (default)
examples:
3.5mm+1.2mil
enter EOF (control D) to end program
EOF
while( 1 ) {
print "> ";
my $line = <>;
if( ! $line ) {
last;
}
chomp $line;
chomp $line;
if( $line ne "" ) {
$line =~ s/([0-9]*(\.[0-9]*)?)mm/($1\/0.000254)/g;
$line =~ s/([0-9]*(\.[0-9]*)?)mil/($1\/0.01)/g;
$line =~ s/([0-9]*(\.[0-9]*)?)pcb/($1\/1.0)/g;
$line =~ s/[^0-9.()*\/+-]//g; # this is important for letting through only arithmetic expressions
my $result = eval( $line );
if( defined $result ) {
printf( "%f\n%fmm\n%fmil\n%fpcb\n",
$result,
$result * 0.000254,
$result * 0.01,
$result * 1.0 );
} else {
print( "error\n" );
}
} # if( $line ne "" )