Why is this program valid? I was trying to create a syntax error
Perl has a syntax called "indirect method notation". It allows
Foo->new($bar)
to be written as
new Foo $bar
So that means
Syntax error ! exit 0;
is the same as
error->Syntax(! exit 0);
or
error->Syntax(!exit(0));
Not only is it valid syntax, it doesn't result in a run-time error because the first thing executed is exit(0)
.
I don't know why, but this is what Perl makes of it:
perl -MO=Deparse -w yuck
BEGIN { $^W = 1; }
use warnings;
use strict 'refs';
'error'->Syntax(!exit(0));
yuck syntax OK
It seems that the parser thinks you're calling the method Syntax
on the error
-object... Strange indeed!
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