Why is this program valid? I was trying to create a syntax error

后端 未结 6 1294
不思量自难忘°
不思量自难忘° 2020-11-28 17:04

I\'m running ActiveState\'s 32 bit ActivePerl 5.14.2 on Windows 7. I wanted to mess around with a Git pre-commit hook to detect programs being checked in with syntax errors.

6条回答
  •  旧时难觅i
    2020-11-28 17:38

    The reason you do not get an error is that the first executed code is

    exit(0);
    

    Because you did not have a semicolon on the first line:

    Syntax error!
    

    The compiler will guess (incorrectly) that this is a subroutine call with a not operator ! thrown in. It will then execute the arguments to this subroutine, which happens to be exit(0), at which point the program exits and sets errorlevel to 0. Nothing else is executed, so no more runtime errors are reported.

    You will notice that if you change exit(0) to something like print "Hello world!" you do get an error:

    Can't locate object method "Syntax" via package "error" ...
    

    and your error level will be set:

    > echo %errorlevel%
    255
    

提交回复
热议问题