Skip to content

Instantly share code, notes, and snippets.

@ChemicalJames
Created October 24, 2012 00:47
Show Gist options
  • Save ChemicalJames/3943037 to your computer and use it in GitHub Desktop.
Save ChemicalJames/3943037 to your computer and use it in GitHub Desktop.
Temperature Module
#!/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;
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