Perl Script Error

Errors

Compile-time error: Perl compiles the entire program before it starts exe-cution. If there is a syntax error anywhere in the program, the compiler prints an error message and stops without attempting to run the program.

Run-time error
: If the program compiles successfully, it will start executing,but if anything goes wrong during execution, the run-time system prints an error message and stops the program.

Semantic error:
In some cases, the program compiles and runs without any errors, but it doesn't do what the programmer intended. Of course, only the programmer knows what was intended, so semantic errors are in the  eye of the beholder.

To see an example of a compile-time error, try spelling print wrong. When you try to run the program, you should get a compiler message like this:
String found where operator expected at ./echo.pl line 3,
near "prin "@ARGV\n""
(Do you need to predeclare prin?)
syntax error at ./echo.pl line 3, near "prin "@ARGV\n""

Execution of ./echo.pl aborted due to compilation errors.

The message includes a lot of information, but some of it is difficult to interpret, especially when you are not familiar with Perl. As you are experimenting with a new language, I suggest that you make deliberate errors in order to get familiar with the most common error messages.

As a second example, try misspelling the name of a variable. This program:
print "@ARG\n";
yields this output:
$ ./echo.pl command line arguments
$
Since there is no variable named @ARG, Perl gives it the default value, which is the empty list. In effect, Perl ignores what is almost certainly an error and tries to run the program anyway. This sort of behavior is occasionally helpful, but normally we would like the compiler to help us find errors, not obscure them.

We can use the strict pragma to change the compiler's behavior. A pragma is a module that controls the behavior of Perl. To use the strict pragma, add the following line to your program:
use strict;
Now if you misspell the name of a variable, you get something like this:
Global symbol "@ARG" requires explicit package name.
Like many compiler messages, this one is misleading, but it contains hints about
where the problem is, if nothing else.

No comments:

Post a Comment