English 中文(简体)
在 Perl 的一行代码末尾加上 { 的意思是什么?
原标题:What does a { at the end of a Perl one-liner mean?
  • 时间:2010-02-22 18:36:12
  •  标签:
  • perl

我亲眼看到了这个一角。

perl -lane  $_{$F[0]}+=$F[1]}print"$_ $_{$_}"for keys%_;{  file

如何根据另一列中的值对列中的值进行求和?这里

and I don t remember how the "{" at the end works. Could someone explain how it works?

最佳回答

来自Perl 帮助:

-n   assume "while (<>) { ... }" loop around program

这只是一个纯文本操作,所以它会得到这个程序:

while (<>) { $_{$F[0]}+=$F[1]}print"$_ $_{$_}"for keys%_;{ }

这是对-n开关的滥用,因为由于原始程序中没有匹配的},while循环被提前关闭。但是由-n开关添加的闭合}仍然需要匹配一个东西,这就是为什么即使它不执行任何操作,程序末尾仍需要额外的{

换句话说,最后一个{的唯一原因是为了避免语法错误。

问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签