use strict; use warnings; use Regexp::Common; # From http://prometheus.frii.com/%7Egnat/yapc/2000-stages/slide31.html # Fixed a typo in the regex (Nathan forgot to escape the forwardslash.) while () { chomp; my $celsius = my $fahrenheit = $_; #was $fahrenheit =~ s|(\d+)C|($1*9/5)+32 . "F"|ge; # But that didn't work with decimals #was $fahrenheit =~ s|(\d+\.\d+)C|($1*9/5)+32 . "F"|ge; #but that required decimals #This is getting ridiculous, so let's just use Damian Conway's Regexp::Common $fahrenheit =~ s|($RE{num}{real})C|($1*9/5)+32 . "F"|ge; print "$celsius is $fahrenheit\n"; } #Outputs: #-2C is 28.4F #-1.5C is 29.3F #-1C is 30.2F #-1.0C is 30.2F #0C is 32F #0.0C is 32F #.1C is 32.18F #0.1C is 32.18F #1C is 33.8F #+1C is 33.8F #1.5C is 34.7F #+1.5C is 34.7F #2C is 35.6F __DATA__ -2C -1.5C -1C -1.0C 0C 0.0C .1C 0.1C 1C +1C 1.5C +1.5C 2C