Created
October 24, 2012 00:47
-
-
Save ChemicalJames/3943037 to your computer and use it in GitHub Desktop.
Temperature Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use warnings; | |
use Temperature; | |
my $arg = $ARGV[0] || '-c20'; | |
if ($arg =~ /^\-(c|f)((\-|\+)*\d+(\.\d+)*)$/) { | |
my ($deg, $num) = ($1, $2); | |
my ($in, $out) = ($num, $num); | |
if ($deg eq 'c') { | |
$deg = 'f'; | |
$out = Temperature::c2f($num); | |
} else { | |
$deg = 'c'; | |
$out = Temperature::f2c($num); | |
} | |
$out = sprintf('(%0.2f)', $out); | |
$out =~ s/^((\-|\+)*\d+)\.0+$/$1/; | |
print "$out $deg\n"; | |
} else { | |
print "Usage: $0 -[c|f]num\n"; | |
} | |
exit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Temperature; | |
use strict; use warnings; | |
sub f2c { | |
my $f = shift; | |
my $c = (5/9) * ($f - 32); | |
return $c; | |
} | |
sub c2f { | |
my $c = shift; | |
my $f = (9/5 * $c) + 32; | |
return $f; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment