Troubleshooting


The following is a list of problems that you may run into when running your scripts, and suggestions for how to fix them. In addition, one good general tip for troubleshooting is to always use perl -w to execute scripts. The warnings it prints can help you find errors or typos in your code.

Problem: You can’t find perl on your machine.

Solution: From the command prompt, try typing the following:

 $ perl -v

If you get back a "command not found" message, try typing

 $ ls /usr/bin/perl

or

 $ ls /usr/local/bin/perl

If one of those commands shows that you do have perl on your system, check your PATH variable and make sure it includes the directory containing perl. Also check your scripts to make sure you entered the full pathname correctly If you still can’t find it, you may have to download and install perl yourself.

Problem: You get “Permission denied” when you try to run a script.

Solution: Check the permissions on your script.

For a perl script to run, it needs both read and execute permission. For instance,

 $ ./hello.pl Can't open perl script "./hello.pl": Permission denied $ ls -l hello.pl ---x------1 kili          46 Apr 23 13:14 hello.pl $ chmod 500 hello.pl $ ls -l hello.pl -r-x------1 kili          46 Apr 23 13:14 hello.pl $ ./hello.pl Hello, World

Problem: You get a syntax error.

Solution: Make sure each line is terminated by a semicolon.

Unlike shell and some other scripting languages, Perl requires a semicolon at the end of every statement.

Problem: You still get a syntax error.

Solution: Make sure all parentheses match correctly and all blocks are enclosed in curly braces.

You can use the showmatch option in vi, or blink-matching-paren in emacs, to help you make sure you always close your parentheses and braces.

Remember to enclose all blocks with curly braces. Unlike C, perl does not allow one-line statements to represent a block. For instance, you can’t say

 while (<>)    if ( ! /^$/)       print "$_\n";

Problem: You get a syntax error when assigning a value to a scalar variable.

Solution: Make sure you use a “$” in front of all scalar variable names.

Unlike most other programming languages, Perl requires all variable names to start with an identifying character-$ for scalar variables, @ for arrays, and % for hashes. Also remember to use a $ when getting a scalar value from a hash or an array

Problem: You get incorrect results when comparing numbers or strings.

Solution: Make sure you are using the right test operators.

Remember that the operators eq and ne are string comparisons, and == and != are numeric comparisons.

Problem: Data received from external sources (such as STDIN) causes unexpected behavior.

Solution: Make sure you chomp your input to remove the newline at the end of strings.

If you forget to chomp data, you will get unexpected newlines when printing and test comparisons will fail.

Problem: Values outside parentheses seem to get lost.

Solution: Group all arguments to a function in parentheses.

Remember that many commands in Perl are functions. Although they are not always required, parentheses are used to group the input to functions. For example, just as

 sqrt (1+2)*3

will take the square root of 1+2 and then multiply the result by 3,

 $ perl -e 'print (1+2)*3' 3

will print 1+2 and then try to multiply the result by 3. Adding parentheses around the arguments to print, as in

 $ perl -e 'print ((1+2)*3)' 9

will solve this problem.

Running your scripts with perl -w will help detect these errors.

Problem: You get the warning “Use of uninitialized value”.

Solution: You may be trying to use a variable that’s undefined.

Some operations should not be done to a variable that’s undefined. For example,

 die "Error: filename argument expected" if (! -e $ARGV[0]);

looks for a file named $ARGV[0]. If it is undefined (because there were no command-line arguments), perl -w will generate a warning.

Problem: Running perl from the command line gives an error message or no output at all.

Solution: Make sure you are enclosing your instructions in single quotes, as in

 $ perl -e 'print "Hello, World!\n"'

Problem: Running your perl script gives unexpected output.

Solution: Make sure you are running the right script!

This might sound silly, but one classic mistake is to name your script “test” and then run it at the command line only to get nothing:

 $ test $

The reason is that you are actually running /bin/test instead of your script. Try running your script with the full pathname (e.g., /home/kili/PerlScripts/test.pl) to see if that fixes the problem.

Problem: Your program still doesn’t work correctly.

Solution: Try running the perl debugger with the -d switch.

The perl debugger is used to monitor the execution of your code in a step-by-step fashion. When using the debugger, you can set breakpoints at exact lines in your script and then see exactly what is going on at any point during the program execution. Debugging a program can be very useful in locating logical errors.

Alternately, you try running your program with perl -MO=Lint scriptname, which will use the module B::Lint to check for syntax errors that perl -w might miss.

You could also try posting to the newsgroup comp.lang.perl. The readers of that newsgroup can often be very helpful in diagnosing problems. Be sure to read the newsgroup FAQ before posting, to avoid asking questions that have already been answered.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net