root/misc/unitconverter.cgi

Revision 2132 (checked in by miyagawa, 14 years ago)

fixed inch to meter conversion

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use CGI;
5 use HTML::Prototype;
6 use MIME::Base64;
7 use Template;
8
9 my $query = CGI->new;
10    $query->charset("utf-8");
11
12 dispatch($query);
13
14 sub dispatch {
15     my $query = shift;
16
17     if ($query->param('from')) {
18         do_convert($query);
19     } elsif ($query->param('spinner')) {
20         show_spinner($query);
21     } else {
22         show_form($query);
23     }
24 }
25
26 sub do_convert {
27     my $query = shift;
28
29     my $from = validate_unit($query->param('from'));
30     my $to   = validate_unit($query->param('to'));
31
32     my $func = "convert_${from}_${to}";
33     no strict 'refs';
34     my $value = eval { $func->(validate_number($query->param('value'))) };
35     if ($@) {
36         print $query->header, "ERR: Can't do $from to $to conversion: $@";
37         return;
38     }
39
40     my $format = $value > 0 ? '%.4f' : '%.8f';
41     $value = sprintf $format, $value;
42
43     print $query->header, $value;
44 }
45
46 sub validate_unit {
47     my $input = shift;
48     return ($input =~ /^(\w+)$/)[0];
49 }
50
51 sub validate_number {
52     my $input = shift;
53     return ($input =~ /^([\-\+\d\.]+)/)[0];
54 }
55
56 sub convert_usd_jpy {
57     require Finance::Currency::Convert::Yahoo;
58     Finance::Currency::Convert::Yahoo::convert($_[0], 'USD', 'JPY');
59 }
60
61 sub convert_jpy_usd {
62     require Finance::Currency::Convert::Yahoo;
63     Finance::Currency::Convert::Yahoo::convert($_[0], 'JPY', 'USD');
64 }
65
66 sub convert_inch_meter {
67     return 0.0254 * $_[0];
68 }
69
70 sub convert_meter_inch {
71     return $_[0] / 0.0254;
72 }
73
74 sub convert_inch_feet {
75     return $_[0] / 12;
76 }
77
78 sub convert_feet_inch {
79     return $_[0] * 12;
80 }
81
82 sub convert_oz_pound {
83     return $_[0] / 16;
84 }
85
86 sub convert_pound_oz {
87     return $_[0] * 16;
88 }
89
90 sub convert_g_pound {
91     return $_[0] / 453.59237;
92 }
93
94 sub convert_pound_g {
95     return $_[0] * 453.59237;
96 }
97
98 sub convert_sqf_sqm {
99     return $_[0] * 0.09290304;
100 }
101
102 sub convert_sqm_sqf {
103     return $_[0] / 0.09290304;
104 }
105
106 sub show_form {
107     my $query = shift;
108
109     my $tt = Template->new;
110     my $template = show_form_template();
111
112     my $stash;
113     $stash->{prototype} = HTML::Prototype->new;
114     $stash->{cgi}       = $query;
115
116     print $query->header('text/html');
117     $tt->process(\$template, $stash);
118 }
119
120 sub show_form_template {
121     return <<'TEMPLATE';
122 [% SET title = "Unit Converter (Japan and United States)" -%]
123 <html>
124 <head>
125 <title>[% title %]</title>
126 <style>
127 body,input,td { font-size: 11px; font-family: trebuchet MS }
128 input { background: #fff }
129 h1 { font-size: 2em }
130 h2 { font-size: 1.6em }
131 </style>
132 [% prototype.define_javascript_functions %]
133 <script>
134 var url = "[% cgi.url %]";
135 function doConvert(input, from, to, unit) {
136   var spinner = unit + "-spinner";
137   $(to).value = ''; // clear
138   $(spinner).innerHTML = "<img src=\"[% cgi.url %]?spinner=1\" style=\"vertical-align:middle\" />";
139   var query = "value=" + encodeURIComponent(input.value) + "&from=" + from + "&to=" + to;
140   new Ajax.Request(
141     url,
142     { method: 'get',
143       parameters: query,
144       onComplete: function(req) { $(to).value = req.responseText; $(spinner).innerHTML = ""; } }
145   );
146 }
147 </script>
148 </head>
149 <body>
150 <h1>[% title %]</h1>
151
152 <h2>Currency</h2>
153 <p>
154 <input type="text" size="12" id="usd" onchange="doConvert(this, 'usd', 'jpy', 'currency')" /> USD =
155 <input type="text" size="12" id="jpy" onchange="doConvert(this, 'jpy', 'usd', 'currency')" /> JPY
156 <span id="currency-spinner"></span>
157 </p>
158
159 <h2>Length</h2>
160 <p>
161 <input type="text" size="12" id="feet" onchange="doConvert(this, 'feet', 'inch', 'length');doConvert(this, 'inch', 'meter', 'length')" /> feet =
162 <input type="text" size="12" id="inch" onchange="doConvert(this, 'inch', 'meter', 'length');doConvert(this, 'inch', 'feet', 'length')" /> inches =
163 <input type="text" size="12" id="meter" onchange="doConvert(this, 'meter', 'inch', 'length');doConvert(this, 'inch', 'feet', 'length')" /> meters
164 <span id="length-spinner"></span>
165 </p>
166
167 <h2>Weight</h2>
168 <p>
169 <input type="text" size="12" id="pound" onchange="doConvert(this, 'pound', 'oz', 'weight');doConvert(this, 'pound', 'g', 'weight')" /> pounds =
170 <input type="text" size="12" id="oz" onchange="doConvert(this, 'oz', 'pound', 'weight');doConvert(this, 'pound', 'g', 'weight')" /> oz =
171 <input type="text" size="12" id="g" onchange="doConvert(this, 'g', 'pound', 'weight');doConvert(this, 'pound', 'oz', 'weight')" /> g
172 <span id="weight-spinner"></span>
173 </p>
174
175 <h2>Dimensions</h2>
176 <p>
177 <input type="text" size="12" id="sqf" onchange="doConvert(this, 'sqf', 'sqm', 'dimensions')" /> square feet =
178 <input type="text" size="12" id="sqm" onchange="doConvert(this, 'sqm', 'sqf', 'dimensions')" /> m<sup>2</sup>
179 <span id="dimensions-spinner"></span>
180 </p>
181
182
183 TEMPLATE
184 }
185
186 sub show_spinner {
187     my $query = shift;
188     print $query->header('image/gif'), decode_base64(<<IMAGE);
189 R0lGODlhEAAQAAAAACH/C05FVFNDQVBFMi4wAwH//wAh+QQLBgAPACwAAAAAEAAQAIMvLy92dnaU
190 lJSlpaWtra21tbW9vb3GxsbOzs7W1tbe3t7n5+fv7+/39/f///////8EifDJ52QJdeq3QnoC4DSF
191 snFAAAIPMizaAheAEbrI0yyOEwSGRkCAKAwIO2PjoQikGktK4zAowCauzCTxkRgGYNNmajBQBWix
192 hlEoEAyTBUIrUVwlUwFy8z0wKEYIUwkNDApBCUdLdhwDCUULjg8MCVokBQ4HBA9UfxsLBDCaIwRd
193 GiMSCAYVDVoRACH5BAsGAA8ALAAAAAAQABAAgz09PXNzc4uLi6Wlpa2trbW1tb29vcbGxs7OztbW
194 1t7e3ufn5+/v7/f39////////wSL8Mnn5Ah16reCegHgNICxPUwQgMAjAIe2MM8BhwmwNovjC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sbIxIDAhVRExEAIfkECwYADwAsAAAAABAAEACDPj4+b29vkJCQpaWl
223 ra2ttbW1vb29xsbGzs7O1tbW3t7e5+fn7+/v9/f3////////BIrwyeckMnXqxwp7B+E0hbI9DWGA
224 xIMMi7Z8yqCEzICgi+MUtoYBwVgoCI0FsMExDAqNzOjwjE0OgswkkZgEAIDAbtM4GAzYgPpw6hRU
225 24JWorBKGAIwU+M8fBxfAgsCBAwMCQYNCQNIIGwJAG8CNV2HWg1qDgMCD04fGwtiD5sODARdGyMS
226 ThVRExEAIfkECwYADwAsAAAAABAAEACDPz8/c3NzkJCQpaWlra2ttbW1vb29xsbGzs7O1tbW3t7e
227 5+fn7+/v9/f3////////BIfwyeckMnXqxwp7B+E0hbI9DWGAxIMMi3aYrxIyA4IujhMAA0bgwFgo
228 CI1FYdB4LAQAAKNJaRwGhdjkIMhMEomJIEAOb6wGw+EXPZw6BdVEgdkotJLGABCgTgwDRBQ/QSoM
229 DHQNCQNILjoKQwZZA2EMCV4NAl0GLVcfG09hnCMEZhojFnUNXhEAIfkECwYADwAsAAAAABAAEACD
230 Li4ub29vjY2NpaWlra2ttbW1vb29xsbGzs7O1tbW3t7e5+fn7+/v9/f3////////BI3wyeckMnXq
231 pwB6B+E0hbI9DACAxIMMi5aYB0DczPA1i+MEtkYgkFgoCLzCoPFIAAMNJqVxGBRik4MgM0kkJgOB
232 2LShGgzaIfHEKBQIhskCwZUosJKGQSydGAYHDBQDAQUMBwkNDAoGDQkDSC5fCwMJCFeVKAlcJAUO
233 ISADghsLBDEhIwRfGyMWGA8NXBEAOw==
234 IMAGE
235 }
Note: See TracBrowser for help on using the browser.