in reply to "possible typo" warnings in modules

I'm no expert at this, but it appears to me that at compile time, the Perl interpreter only scans the modules for "syntax errors", it dosn't eval every statement to see if they are good in reality.
my $val = $Some:Nonexistent::Pkg::variable; # a missing :
or
my $val = $Some::Nonexistent::Pkg::variable; my $val; print $val;
will yield errors.

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: "possible typo" warnings in modules
by diotalevi (Canon) on Feb 17, 2007 at 18:54 UTC

    You are wrong. There is no separate check for "syntax errors" and later for "evaling." It sounds like your mental model of perl's internal workings are far off. Perl compiles into the same form, always. This form is directly executable. Code that is present at a module's file or "top" level is run just as soon as the file has finished being compiled.

    I have the impression that you think perl is executing the statements by uh "evaling" them as source code at runtime kind of like bash or DOS batch files. That is far from the truth. The directly executable form I already mentioned is closer to being like an internal or hidden lisp. As perl reaches each instruction, it just follows a C function pointer to run each instruction. Every instruction returns the address of the next instruction in memory so the runloop does this very much like for ( $op = START; $op = $op->(); ){}.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Well how do you account for the behaviour that there are no errors when the syntax is correct, but an error when the syntax is wrong ( like a missing colon or scalars define twice with my)? Then if the syntax is ok, it lets it run and evaluation errors appear at runtime?

      UPDATE added example

      For instance this will stop at compile time:

      #!/usr/bin/perl use strict; use warnings; use lib '.'; use WarnMe; print "1\n"; print $WarnMe::val,"\n"; print "2\n";
      package WarnMe; use strict; use warnings; # syntax error my $val = $Some:Nonexistent::Pkg::variable; 1;
      outputs:
      zentara@:zentara$ ./WarnMe Global symbol "$Some" requires explicit package name at WarnMe.pm line + 7. syntax error at WarnMe.pm line 7, near "$Some:" Compilation failed in require at ./WarnMe line 5. BEGIN failed--compilation aborted at ./WarnMe line 5.

      Clearly the Perl compiler is checking syntax first. If you fix that syntax error, the script will compile and run with issuing a non-fatal runtime error:

      Name "WarnMe::val" used only once: possible typo at ./WarnMe line 8. 1 Use of uninitialized value in print at ./WarnMe line 8. 2

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum

        Your first example throws all the proper errors. I don't know how to replicate your environment to get that to stop throwing warnings. Your second example also throws the proper warnings.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^2: "possible typo" warnings in modules
by ikegami (Patriarch) on Feb 17, 2007 at 21:04 UTC

    it dosn't eval every statement to see if they are good in reality.

    First of all, it can't. For example, how could it run a statment before its containing block has finished compiling?

    Secondly, it's impossible to detect most programming errors. For a system to check for errors, it would have to know what you are trying to do, but if it knew what you wanted to do, there would be no need to write a program to tell it what to do. It is possible for the system to look for patterns that usually result in errors. When Perl detects such a pattern, it issues a warning. Some at compile-time, some at run-time.

    Finally, how does your post relate to its parent post?