开发者

How does the Perl's Slurp module work?

I had a look at the source of Slurp and I would love to understand how does slurp() work:

sub slurp { 
    local( $/, @ARGV ) = ( wantarr开发者_Go百科ay ? $/ : undef, @_ ); 
    return <ARGV>;
}

Where is the file even opened?


See ARGV and $/ in perldoc perlvar.

See also Path::Class:File::slurp.


ARGV is a handle, the file has been opened implicitly.


This snippet puts the filename in @ARGV. The ARGV filehandle implicitly opens the files it sees in @ARGV. This is the same filehandle that we don't see in the diamond operator <> since it's the default filehandle for that operator.

This is the same Perl idiom as:

 my $data = do { local( @ARGV, $/ ) = $file; <> };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜