What is "Modern Perl"?
I have been heari开发者_如何学Gong a lot about this "Modern Perl." What is it?
One of the things I heard was the new open syntax:
open my $FH, '<', $filename
and not
open FH, "<$filename";
What else is in Modern Perl?
To quote the source
Modern Perl programming, circa 2010, relies on the collected wisdom of the entire Perl ecosystem. It's time to write elegant, reliable, maintainable, well-tested, and predictable code.
See also, the book. And this quote from the book
Modern Perl is a loose description of how experienced and effective Perl 5 programers work. They use language idioms. They take advantage of the CPAN. They're recognizably Perlish, and they show good taste and craftsmanship and a full understanding of Perl.
To add some specifics to deinst's overview, Modern Perl:
- uses Perl 5.10's new features, like switch statements (
given
/when
) andsay
- follows good Perl programming practices, like
use strict
anduse warnings
- may use the Modern::Perl CPAN module to streamline all of this
- uses Moose for writing high-level OO code
Modern Perl isn't a proper noun; it's just something people might say to refer to Perl code that uses features only available in the last X years, where X will vary from person to person.
For information about various changes to Perl, see the perldelta files, for instance at http://perldoc.perl.org/index-history.html.
In order to be specific to your question related to opening a file handle in modern Perl: You should use the three-argument form, instead two-argument form!
Use the three-argument form of open
to specify I/O layers (also called "disciplines") to apply to the handle. It affects how read-write is processed (see http://perldoc.perl.org/functions/open.html for more details). For example:
open(my $fh, "<:encoding(UTF-8)", "filename")
|| die "can't open UTF-8 encoded filename: $!";
精彩评论