简体   繁体   English

Perl One衬里括号内的函数名称

[英]Function name inside parentheses in Perl one liner

I'm working on a Perl one liner tutorial and there are one liners like this: 我正在研究Perl的一个衬板教程,并且有一个这样的衬板:

ls -lAF | perl -e 'while (<>) {next if /^[dt]/; print +(split)[8] . " size: " . +(split)[4] . "\n"}'

You see the function name split has been inside parentheses. 您会看到函数名称拆分已放在括号内。 Documentation about this use of functions is hard to find on Google so I couldn't find any information on it. 在Google上很难找到有关此功能使用的文档,因此我找不到任何信息。 Could somebody explain it? 有人可以解释吗? Thank you. 谢谢。

It probably doesn't help that the use of split is defaulting everything - it's splitting $_ by spaces and returning a list of values. 使用split会默认使用默认值,这可能无济于事-将$_用空格分割并返回值列表。

The (...)[8] is called a list slice, and it filters out all but the 9th value returned by split . (...)[8]被称为列表切片,它过滤掉split返回的第9个值以外的所有值。 The preceding plus is there to prevent Perl from misparsing the brackets as being part of a function call. 前面的加号是为了防止Perl将括号误认为是函数调用的一部分。 Which also means you don't need it on the second instance. 这也意味着您无需在第二实例上使用它。

So print +(split)[8]; 因此, print +(split)[8]; is basically a very succinct way of writing 基本上是一种非常简洁的写作方式

my @results=split(/ /,$_);
print $results[8]; 

The example you've included is performing the split twice so it might be more efficient to do the more verbose version as you can get $results[4] from the above without any extra effort. 您所包含的示例执行了两次split ,因此执行更详细的版本可能会更有效,因为您可以从上面获取$results[4]而无需付出额外的努力。

Or because you can put a list of indexes inside the [] , you could do the split once and use printf to format the output like this 或者因为您可以在[]内放置一个索引列表,所以可以一次分割并使用printf格式化输出,如下所示

printf "%s size: %s\n", (split)[8,4];

In my opinion you should be avoiding this author's advice, both for the reasons laid out in my comments on your question, and because they don't appear to know their topic at all well. 我认为,出于我对您的问题的评论中提到的原因,以及他们似乎根本不了解其主题,您应该避免使用该作者的建议。

The original "one-liner" was this 原始的“单线”是这个

ls -lAF | perl -e 'while (<>) {next if /^[dt]/; print +(split)[8] . " size: " . +(split)[4] . "\n"}'

This could be written much more succinctly by using the -n and -a options, giving this 通过使用-n-a选项,可以更简洁地编写该代码,

ls -lAF | perl -wane 'print $F[8] size: $F[4]\n" unless /^[dt]/'

Even without the "luxury" of these options you could write 即使没有这些选项的“豪华”,您也可以编写

ls -lAF | perl -e '/^[dt]/ or printf "%s size: %s\n", (split)[8,4]  while <>'

I recommend that you go and read the Camel Book several times over the next few years. 我建议您在接下来的几年中多次阅读骆驼书 That is the best way to learn the language that I have found. 那是学习我发现的语言的最好方法。

Most installations of Perl include a full set of documentation, accessible using the perldoc command. 大多数Perl安装都包含一整套文档,可使用perldoc命令访问。

You need to read the Slices section of perldoc perldata which makes very clear this use of slicing. 您需要阅读perldoc perldataSlices部分,这清楚地说明了切片的用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM