in reply to SOLVED: Eval not trapping fatal error
G'day Ea,
I see ++Corion has given plenty of information on why you can't assign to a constant.
Constants, such as the PI you have in your code, are subroutines with a prototype of '()' which allows them to be inlined (see "perlsub: Constant Functions"); as such, they can be redefined.
I really don't recommend that you use the following in any production code but, purely as an academic exercise, consider this:
#!/usr/bin/env perl use strict; use warnings; use constant PI => 3.14; eval { no warnings 'redefine'; sub PI () { 3 } }; warn "You can't round down PI to 3" if $@; print 'PI now set to ', PI, "\n";
When run, this outputs:
PI now set to 3
No errors or warnings are emitted.
— Ken
|
---|