Fancy file slurping in Perl

為{幸葍}努か 提交于 2019-11-30 03:12:26

问题


I was looking into efficient ways to read files in Perl and came across this very interesting one liner:

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

My question is: How exactly does this work? Normally when slurping a file you set $/ = undef, but I don't see how this does that. This little piece of code is proving to be very difficult to wrap my head around.

What would be a simplified breakdown and explanation for this?


Now that I know how it works, lets get real fancy!

Not that this code has any real use; it's just fun to figure out and cool to look at. Here is a one-liner to slurp multiple files at the same time!!!

my @texts = map { local (@ARGV, $/) = $_; <> } @files;

回答1:


local (@ARGV, $/) = $file;

is the same as

local @ARGV = ( $file );
local $/    = undef;

<> then reads from files mentioned in @ARGV, i.e. from $file.



来源:https://stackoverflow.com/questions/30062413/fancy-file-slurping-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!