请问这个perl的一个在bash的衬垫的作品? [英] How does this perl one liner in the bash works?

查看:106
本文介绍了请问这个perl的一个在bash的衬垫的作品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找的,我可以如何根据每个句子的长短排序文件,我对面这个<一个这个片段来了href=\"http://stackoverflow.com/questions/12417403/how-to-sort-the-lines-in-a-file-from-shortest-to-longest\">answer

I was looking on how I could sort a file based on the length of each sentence and I came across this snippet from this answer

perl -ne 'push @a, $_ } { print sort { length $a <=> length $b } @a' input
                      ^ ^  

我测试了它和它的作品,但我没有一个线索是如何工作的!据我所看到的语法是错误的。它有一个开放的右括号和非封闭我标志着右括号。结果
我真的有麻烦搞清楚如何在bash结果像这样运行perl的命令
可能有人请解释这个片段?

I tested it and it works but I don't have a clue how this works! As far as I can see the syntax is wrong. It has an open right bracket and a non closed right bracket which I have marked.
I am really having trouble figuring out how to run perl commands like this in bash
Could some one please explain this snippet?

推荐答案

} {就是所谓的蝴蝶或的 爱斯基摩人的问候 阿比盖尔发现,在1997年。

}{ is so called butterfly or Eskimo greeting Discovered by Abigail, in 1997.

基本上它关闭由 -n 实行,而循环开关,接下来} { while循环后的程序段执行。

Basically it closes while loop imposed by -n switch, and what follows }{ is block executed after while loop.

这就是Perl解析这一行,

This is how perl parses this line,

perl -MO=Deparse -ne 'push @a, $_ } { print sort { length $a <=> length $b } @a' input


LINE: while (defined($_ = <ARGV>)) {
    push @a, $_;
}{ # <==here it is

    print((sort {length $a <=> length $b;} @a));
}

一个比较常见的方法就是使用的 END {} 块而不是} {运营商:

A more common way would be using END{} block instead of }{ operator:

perl -ne '
  push @a, $_ 
  END { 
    print sort { length $a <=> length $b } @a
  }
' input

这篇关于请问这个perl的一个在bash的衬垫的作品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆