perlの0除算は引き起こし易い誤りですが、強制終了(die)に繋がり、影響が大きいerrorです。
せめて...0除算による強制終了は避けたい...場合、overload::constant で整数や浮動小数をMath::BigIntやMath::BigFloatに置換するとgracefulなerrorとできます。
sample script.
#!/usr/local/bin/perl use strict; use warnings; use utf8; use Math::BigInt; use Math::BigFloat; use Data::Dumper; BEGIN { overload::constant (integer => sub {return Math::BigInt->new(shift)}, float => sub {return Math::BigFloat->new(shift)}); } main(); sub main { for my $vals ([2, 1 ],[2, 0 ], [2.1,1 ],[2.1,0 ], [2, 1.0],[2, 0.0]){ print "$vals->[0] / $vals->[1] = " , $vals->[0] / $vals->[1] , "\n"; } } __END__
実行結果
$ ./foo3.pl 2 / 1 = 2 2 / 0 = inf 2.1 / 1 = 2.1 2.1 / 0 = inf 2 / 1 = 2 2 / 0 = inf