Learning Perl: 4.8. The use strict Pragma
2010-07-21 17:33
435 查看
4.8. The use strict PragmaPerl tends to be a permissive language.[] But maybe you want Perl to impose a little discipline; that can be arranged with the use strict pragma. [ ] Bet you hadn't noticed. A pragma is a hint to a compiler, telling it something about the code. In this case, the use strict pragma tells Perl's internal compiler that it should enforce some good programming rules for the rest of this block or source file. Why would this be important? Well, imagine that you're composing your program, and you type a line like this one: $bamm_bamm = 3; # Perl creates that variable automatically Now, you keep typing for a while. After that line has scrolled off the top of the screen, you type this line to increment the variable: $bammbamm += 1; # Oops! Since Perl sees a new variable name (the underscore is significant in a variable name), it creates a new variable and increments that one. If you're lucky and smart, you've turned on warnings, and Perl can tell you that you used one or both of those global variable names once in your program. But if you're merely smart, you used each name more than once, and Perl won't be able to warn you. To tell Perl you're ready to be more restrictive, put the use strict pragma at the top of your program (or in any block or file where you want to enforce these rules): use strict; # Enforce some good programming rules Now, among other restrictions,[*] Perl will insist that you declare every new variable, usually done with my:[ ] [*] To learn about the other restrictions, see the documentation for strict. The documentation for any pragma is filed under that pragma's name, so the command perldoc strict (or your system's native documentation method) should find it for you. In brief, the other restrictions require that strings be quoted in most cases, and that references be true (hard) references. Neither of these restrictions should affect beginners in Perl. [ ] There are some other ways to declare variables, too. my $bamm_bamm = 3; # New lexical variable If you try to spell it the other way, Perl can complain that you haven't declared any variable called $bammbamm, so your mistake is automatically caught at compile time. $bammbamm += 1; # No such variable: Compile time fatal error Of course, this applies only to new variables; you don't need to declare Perl's built-in variables, such as $_ and @_.[ ] If you add use strict to a previously written program, you'll generally get a flood of warning messages, so it's better to use it from the start when it's needed. [ ] In some circumstances, you don't want to declare $a and $b because they're used internally by sort. So, if you're testing this feature, use other variable names than those two. The fact that use strict doesn't forbid these two is one of the most frequently reported non-bugs in Perl. Most people recommend that programs that are longer than a screenful of text generally need use strict. And we agree. From here on, most (but not all) of our examples will be written as if use strict is in effect even where we don't show it. That is, we'll generally declare variables with my where it's appropriate. Though we don't always do so here, we encourage you to include use strict in your programs as often as possible. |
相关文章推荐
- Learning Perl: 2.8. The chomp Operator
- Perl use strict 控制变量
- perl use vars pragma使用技巧
- Perl Learning: 2.9. The while Control Structure
- You need to install the perl-doc package to use this program
- Learning Perl: 4.9. The return Operator
- How to Use the Keras Functional API for Deep Learning
- You need to install the perl-doc package to use this program.
- Perl Learning: 2.6. The if Control Structure
- Learning Perl: 3.6. The foreach Control Structure
- Learning Perl: 5.2. Input from the Diamond Operator
- perl use vars pragma使用
- strict pragma in Perl
- HowTo use the CDT to navigate Linux kernel source
- learning about Project Management -A01- The Mythical Man-Month
- How to Use the Visual C++ DEBUGGER
- use tomcat to access the file cross the environment
- Post-quantum key exchange for the TLS protocol from the ring learning with errors problem
- The sequence of the learning ORACLE.
- 问题: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no