花哨的文件在Perl中sl </s> [英] Fancy file slurping in Perl
问题描述
my $ text我正在研究有效的方法来读取Perl中的文件。 = do {local(@ARGV,$ /)= $ file; <> };
我的问题是:这个工作到底如何?正常情况下,当你设置一个文件 $ / = undef
,但我不明白这是怎么回事。这个小小的代码被证明是很难包围我的头。
这个简单的分解和解释是什么?
现在我知道它是如何工作的,让我们看看真实的想法!
代码有任何实际的用途;想一想就好,看起来很酷。这是一个单线程,同时啜饮多个文件!!!
my @texts = map {local(@ ARGV,$ /)= $ _; <> } @files;
local @ARGV,$ /)= $ file;
与
$ b $
local @ARGV =($ file);
local $ / = undef;
<>
在 @ARGV
中提到,即从 $ file
。
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;
local (@ARGV, $/) = $file;
is the same as
local @ARGV = ( $file );
local $/ = undef;
<>
then reads from files mentioned in @ARGV
, i.e. from $file
.
这篇关于花哨的文件在Perl中sl </s>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!