企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# What's Missing Perl isn't perfect, but it is malleable—in part because no single configuration is ideal for every programmer and every purpose. Some useful behaviors are available as core libraries. More are available from the CPAN. Effective Perl programmers take full advantage of the options available to them. ### Missing Defaults Perl's language design process has always tried to combine practicality with expandability, but it was as impossible to predict the future in 1994 as it is in 2014. Perl 5 expanded the language and made the CPAN possible, but it also retained backwards compatibility with most Perl 1 code written as far back as 1987. The best Perl code of 2014 is very different from the best Perl code of 1994 or the best Perl code of 1987. Although Perl includes an extensive core library, it's not comprehensive. Many of the best modules are available outside of the core, from the CPAN ([The CPAN](#)). The `Task::Kensho` meta-distribution includes several other distributions which represent the best the CPAN has to offer. When you need to solve a problem, look there first. There are still plenty of gems in the core, however. ### The strict Pragma The `strict` pragma ([Pragmas](#)) allows you to forbid (or re-enable) various powerful language constructs which offer potential for accidental abuse. `strict` forbids symbolic references, requires variable declarations ([Lexical Scope](#)), and prohibits the use of undeclared barewords ([Barewords](#)). While symbolic references are occasionally necessary ([Using and Importing](#)), the use of a variable as a variable name offers the possibility of subtle errors of action at a distance—or, worse, the possibility of poorly-validated user input manipulating private data for malicious purposes. Requiring variable declarations helps to detect typos in variable names and encourages proper scoping of lexical variables. It's easier to see the intended scope of a lexical variable if all variables have `my` or `our` declarations in the appropriate scope. `strict` takes effect in lexical scopes. See `perldoc strict` for more details. ### The warnings Pragma The `warnings` pragma ([Handling Warnings](#)) controls the reporting of various warning classes, such as attempting to stringify the `undef` value or using the wrong type of operator on values. It also warns about the use of deprecated features. The most useful warnings explain that Perl had trouble understanding what you meant and had to guess at the proper interpretation. Even though Perl often guesses correctly, disambiguation on your part will ensure that your programs run correctly. The `warnings` pragma takes effect in lexical scopes. See `perldoc perllexwarn` and `perldoc warnings` for more details. Asking for More Help If you use both `warnings` with `diagnostics`, you'll get expanded diagnostic messages for each warning present in your programs, straight out of `perldoc perldiag`. It's a great help when learning Perl, but be sure to disable `diagnostics` before deploying your program, lest you fill up your logs or expose debugging information to users. ### `IO::File` and `IO::Handle` Before Perl 5.14, lexical filehandles were objects of the `IO::Handle` class, but you had to load `IO::Handle` explicitly before you could call methods on them. As of Perl 5.14, lexical filehandles are instances of `IO::File` and Perl loads it for you. Add `use IO::Handle;` to code running on Perl 5.12 or earlier if you call methods on lexical filehandles. ### The autodie Pragma Perl leaves error handling (or error ignoring) up to you. If you forget to check the return value of every `open()` call, for example, you could try to read from a closed filehandle—or worse, lose data as you try to write to one. The `autodie` pragma changes this for you. If you write: ~~~ use autodie; open my $fh, '>', $file; ~~~ ... an unsuccessful `open()` call will throw an exception. Given that the most appropriate approach to a failed system call is throwing an exception, this pragma can remove a lot of boilerplate code and allow you the peace of mind of knowing that you haven't forgotten to check a return value. See `perldoc autodie` for more information. ### Perl Version Numbers If you encounter a piece of Perl code without knowing when it was written or who wrote it, can you tell which version of Perl it requires? If you have a lot of experience with Perl both before and after the release of Perl 5.10, you might remember which version added `say` and when `autodie` entered the core. Otherwise, you might have to guess, trawl through `perldelta` files, or use `CPAN::MinimumVersion` from the CPAN. There's no requirement for you to add the minimum required Perl version number to all new code you write, but it *can* clarify your intentions. For example, if you've tested your code with Perl 5.12 and use only features present in Perl 5.12, write: ~~~ use 5.012; ~~~ ... and you'll document your intent. You'll also make it easier for tools to identify the particular features of Perl you may or may not use in this code. If someone comes along later and proves that the code works just fine on Perl 5.10, you can change the version number—and you'll do so based on practical evidence.