Perl Cannot Be Parsed: A Formal Proof (2008)
perlmonks.org
perlmonks.org
The same goes for languages like C, but the difference is that in perl you can define symbols at compilation time as a result of running other code, e.g.:
$ perl -wE 'BEGIN { eval "sub spew { say \@_ }" if time % 2 == 0 } spew "hello"'
hello
$ perl -wE 'BEGIN { eval "sub spew { say \@_ }" if time % 2 == 0 } spew "hello"'
Unquoted string "spew" may clash with future reserved word at -e line 1.
Here the program can be syntactically correct or incorrect depending
on the compilation time.edit: here's the same code in python
from time import time
if round(time())%2==0:
exec("def spew(x): print x")
spew("hello")In Python and other dynamic languages the compilation part is very simple, the code is parsed (the grammar is very simple) and converted to byte-code. Then the byte-code is executed. Things like function and class definitions happen at run-time, not at compile-time.
In Perl 5, the line between compilation-time and run-time is very fuzzy. It doesn't have a simple grammar that you can use to convert Perl to op-code. You can run code at compilation-time and change the way the compiler interprets things.
This comment in the perl parser describes some of the beginnings of that rabbit hole: http://perl5.git.perl.org/perl.git/blob/70f63a4c7dba89e8e48b...
The first comment.
"But that will not happen in practice"
If you think that to be the case, use a language that _can_ be parsed. That makes life easier for implementers of IDE's, refactoring tools, etc. and costs you nothing (given the "that will not happen in practice" claim)
Infact it says: "only Perl can parse Perl".
On the other hand JavaScript has esprima that is written in JavaScript, and also other languages like Python can parse themselfs.
As a Perl programmer, I don't know if it is a pro or a cons. For example Perl::Critic cannot parse some costructs, cause they are too complicated. What I usually do, as in many other contexts, I try to understand what is the subset of features I need to use.