Created
January 3, 2012 19:09
-
-
Save eqhmcow/1556389 to your computer and use it in GitHub Desktop.
safe divide by zero
This file contains hidden or 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 | |
sub divide { | |
($a, $b) = @_; | |
$c = eval { $a / $b }; | |
$@ =~ m/Illegal division by zero/ && rand() > .5 ? ($b = 1) : ($b = -1); | |
$@ && ($c = $a / $b); | |
return $c; | |
} | |
print divide(1,4), "\n"; | |
print divide(1,0), "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment