-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunits
executable file
·174 lines (158 loc) · 4.2 KB
/
units
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl -w
# Sat Aug 27 20:56:49 EDT 2011
(my $email='XXX%YYY,ch')=~ tr/%,/@./;
use strict; use warnings FATAL => 'uninitialized';
use utf8;
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname value[s]
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
usage unless @ARGV;
my $first=$ARGV[0];
usage if $first eq "-h" or $first eq "--help";
utf8::decode($myname);
sub feet2m($) {
my ($v)=@_;
($v * 0.3048)
}
sub m2feet($) { # lol
my ($v)=@_;
($v / 0.3048)
}
my %currency =
(
# 1 X in CHF
# e.g. for c in usd eur gbp cad; do $BROWSER https://www.investing.com/currencies/"$c"-chf; read; done
CHF=> 1,
USD=> 0.8829,
EUR=> 0.9284,
GBP=> 1.1255,
CAD=> 0.6231,
);
our $conv=
+{
"°C"=> sub {
my ($v)=@_;
(($v*9/5)+32)." °F"
},
"°F"=> sub {
my ($v)=@_;
(($v - 32)*5/9)." °C"
},
"feet"=> sub {
my ($v)=@_;
feet2m($v)." m, ".($v * 12). " in"
},
"miles"=> sub {
my ($miles)=@_;
my $feet= $miles * 5280;
feet2m($feet)." m, $feet feet"
},
"km"=> sub {
my ($km)=@_;
my $feet= m2feet($km*1000);
my $miles= $feet / 5280;
"$miles miles"
},
do {
my $f= sub {
my ($gal)=@_;
my $imperial_gallon= 4.54609; # l, used in the United
# Kingdom, Canada, and some
# Caribbean nations
my $US_gallon= 3.785; # l, used in the US and some Latin
# American and Caribbean countries
# While there is no official symbol for the gallon (as
# there are for SI units), gal is in common use.
"$gal gal (imperial) = ".($gal * $imperial_gallon)." l"
.", $gal gal (US) = ".($gal * $US_gallon)." l"
};
(gal=> $f,
gallon=> $f)
},
knot=> sub {
# unit of speed equal to one nautical mile (1.852 km) per
# hour, approximately 1.15078 mph
# a vessel travelling at 1 knot along a meridian travels
# approximately one minute of geographic latitude in one hour.
# Worldwide, the knot is used in meteorology, and in maritime
# and air navigation
my ($knot)=@_;
"$knot knots = ".($knot * 1.852). " km/h"
},
pound=> sub {
my ($pound)=@_;
# https://en.wikipedia.org/wiki/Pound_(mass)#Avoirdupois_pound
# well, is this the right one? for ounces, yes, but otherwise?
# Really, krst.
"$pound Avoirdupois pounds = about ".($pound * 0.454)." kg"
},
cup=> sub {
my ($cup)= @_;
# 6 cup US = 1.42 l
my $l = $cup * 1.42/6;
"$cup cups = about $l l"
},
do {
my $ounce= sub {
my ($ounce)=@_;
# The common avoirdupois ounce (approximately 28.3 g) is 1/16
# of a common avoirdupois pound; this is the United States
# customary and British imperial ounce.
"$ounce ounces = about ".($ounce * 0.454 / 12)." kg"
};
(ounce=> $ounce,
"oz."=> $ounce)
},
do {
my $psi2pa= sub {
my ($psi)= @_;
$psi * 6.894757e3
};
my $atm2pa= sub {
my ($atm)= @_;
$atm * 101325
};
my $toall= sub {
my ($given2pa)= @_;
sub {
my ($given)= @_;
my $Pa = $given2pa->($given);
my $psi = $Pa / 6.894757e3;
my $atm = $Pa / 101325;
"$given $myname = $Pa Pa, $atm atm, $psi psi"
}
};
(psi=> &$toall($psi2pa),
atm=> &$toall($atm2pa),
Pa=> &$toall(sub{$_[0]})
)
},
do {
my $convert = sub {
my ($amount) = @_;
my $from = $myname;
my $to_chf = $currency{$from} // die "unknown currency '$from'";
my $chf = $amount * $to_chf;
for my $cur (sort keys %currency) {
my $to_cur = $currency{$cur};
my $in_cur = $chf / $to_cur;
printf '%s %.2f'."\n", $cur, $in_cur;
}
};
map {
$_ => $convert
} keys %currency
},
}->{$myname}
or die "unknown name '$myname'";
print &$conv ($_)."\n"
or die $!
for @ARGV;
#use Chj::ruse;
#use Chj::Backtrace; use Chj::repl; repl;