in reply to Re: Catching a 'division by zero' error with Exception::Class
in thread Catching a 'division by zero' error with Exception::Class

The exception even happens before the eval block is entered. This is because perl calculates immediate expressions during compile time.

That's only true for older perls:

$ perl5.10.0 -ce '1/0' -e syntax OK $ perl5.8.8 -ce '1/0' Illegal division by zero at -e line 1.

(I think it was intentionally removed in newer perls because it could break code even if it's not reachable; consider this example:

my $DEBUG = 0; ... if ($DEBUG) { $verbosity = 10 / $DEBUG; }

in which case constant folding might break things).

(Update: in that last example there is no constant folding at all, because variables are not constant folded. ikegami suggested to use constant DEBUG => 0; instead, which should better demonstrate what I was thinking about).