Universal Guide of Brian Perl problem of translation: klaus Source: China Association of Perl FPC (Foundation of Perlchina) formerly known as: brian's Guide to Solving Any Perl Problem Author: brian d foy original: http: //www.panix.com/~comdog/ Brian't, 20 2004 Perlchina Reminds you: Please protect the author's copyright and maintain the crystallization of author labor. Title Brian's Perl Problem Harmony Outline Read this guide and keeps wise mind to describe my debug philosophy. I believe three things: This is not a personal problem, don't always think about your code. You may feel that you are an artist, but it is actually an experienced master will also write a lot of garbage. Everyone's code is garbage, I am also yours. To learn it. When you encounter problems, you should think: "Hey, the garbage code I wrote out." This shows that you no longer blame Perl. Should be a personal problem. Forget what you have done before. If you are not your way, you will not read this. This is not a bad thing, but it is only a bit of time. We have experienced. Personal Responsibility If your code has a problem, it is just - your problem. You should do our best to solve it. Remember, everyone has their own code, everyone has their own problems. My own homework is done, and I do my best before trouble. If you have made all the things you can do in this guide, you still can't solve the problem, then you have tried your best, you should find someone else. Change the way you do correctly, don't make the same mistake. It is very likely that you write code method, not the code you write is wrong. Change the way you do before, make life easier. Don't expect Perl to get used to you because it is impossible. You have to get used to Perl. It is just a language, not a way of life. My method: Do you compile code with STRICTURES? If you don't have to strictures, open it. The reason why PERL masters is masters, because they use strict patterns, making them more time to solve other problems, learn new things, and upload modules in pouring CPAN. You can use Strict Pragma to open strictures use strict in your code; you can also use Perl -m switch to open it in the command line: perl -mstrict script.pl You may be angry with it, but insist on using it several worships After that, you will write better code, spend less time to check the low-level errors, and you may no longer need to read this guide. What is WARNING? Perl will give you some problematic structures. Open Warning and let Perl help you. You can open it on the first line with Perl -w switch: #! / Usr / bin / perl -w can also open Warning on the command line: perl -w script.pl You can also use vocabulary warnings, it has many Interesting features. More information See Warnings help documents. Use Warnings; if you don't understand what a warning mean, you can use Warning's detailed mode, or use diagnostics in your code: use Diagnostics; solve the first question first! After you get warnings or error messages from Perl, first solve the first one, then see if Perl is still reported to other errors. Since subsequent errors are likely to be due to the first error derived.
Check the code before the error message line number! Perl is wrong when the error has occurred instead of this. Therefore, when the PERL is reported, the error has occurred, and the wrong place is before this. Look at the code and expression before the wrong line number. Is that variable value as you think? Don't mess! Check if it is correct when using a value in the expression. The best debugger in the world is Print. Print stderr "The value is" The reason I am in brackets) is why the $ value is because I can see if there is a space or wrap on the beginning and the tail. If this value is not a scalar, then I use DATA :: Dumper to print these data structures. Require Data :: Dumper; Print stderr "the hash is", Data :: Dumper :: Dumper (% HASH), "/ N"; if the result is not what you expect, then move to the previous sentence, come back ! Find this value in the last correct location. You can also open the built-in Perl debugger with the Perl-D switch. For more information, please refer to Perldebug. Perl -d script.pl You can also use other debuggers or development environments, want PTKDB (a TK-based graphic debugger) or Komodo (ActiveStates Mozilla-based Perl IDE) Do you use the function? I have written the time of the Perl program is not short, but I still check PerlFunc almost every day. Some things I am not eaten, and sometimes I lack sleep, even if I have no common sense, then I don't know why sprintf () does not print to the screen. You can use the Perldoc command and its -f switch to query a particular function. Perldoc -f function_name If you are using a module, query its documentation, see if you use it in the right way. You can query its document with Perldoc. Perldoc Module :: Name How do you use the special variable? Similarly, I often check Perlvar. However, when I found Perl Quick Reference This book was more convenient, I rarely checked Perlvar. Do you use the module version correct? Some modules will have many changes in the upgraded version. Do you know what version of the module you use? You can check your module with a line of Perl statement: perl -mmodule :: name -le 'print module :: name-> version'; if you read the document is not your machine's local document, it is like http : //www.Perldoc.com or http://search.cpan.org, then you are more likely to touch the problem of document version. Have you tested with a small script? If you are trying new things, or think that a small code is strange, you can write a shortest program to run this piece. This method excludes all other factors. If there is no problem, then the problem may not be in this code. If the test result is not right, then you probably find your problem. Do you check the environment? Some things are dependent on environment variables. Are you sure your environment variable is right? Is the environment variable used when running is the environment variable you see now? Remember that some CGI programs or CRON may be used in the environment variables and the shell, especially on different machines. Perl is storing environmental variables in% env.