On Tuesday 15 Dec 2009 15:53:28 Philip Potter wrote: > 2009/12/15 Shlomi Fish <shlo...@iglu.org.il>: > > On Tuesday 15 Dec 2009 14:25:28 Xiao Lan (小兰) wrote: > >> On Tue, Dec 15, 2009 at 7:50 PM, Xiao Lan (小兰) <practicalp...@gmail.com> > > > > wrote: > >> > I did have tried that, but this will get a runtime error. > >> > >> Sorry this is exactly a compile-time error. > >> > >> # cat except.pl > >> > >> eval { $x=12/0 }; > >> print "0 div error" if $@; > >> > >> # perl -c except.pl > >> Illegal division by zero at except.pl line 2. > >> > >> So, can't we capture it at runtime when meeting such error? > > > > No, you cannot capture compile-time errors at runtime. You'll need to > > make sure your code compiles before you run it. Note that you can capture > > such errors if you do perldoc -f require, perldoc -f do (for a filename), > > string eval "", etc. at run-time, in which case perl 5 invokes its > > compiler to compile some code at run-time. > > How can "Illegal division by zero" be a compile-time error? It seems > clear to me that it's a run-time error, which the optimizer has > (wrongly) decided to raise at compile-time. >
Well, the Perl compiler tends to collapse constants. So if it sees something like: <<< ... ( CONSTANT_EXPR() / 0 ) ... >>> it will try to find the value of "CONSTANT_EXPR() / 0" so it can optimise it as a constant for the interpreter, and then get a fault there. It's a standard optimisation technique that also exists in other compilers such as gcc that are collapsing such constant expressions. So it does get evaluated at compile- time and as such should be avoided. Regards, Shlomi Fish > Phil > -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ The Case for File Swapping - http://shlom.in/file-swap Bzr is slower than Subversion in combination with Sourceforge. ( By: http://dazjorz.com/ ) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/