Perl 让小代码做这么多事情的秘诀是什么?
我见过很多(代码高尔夫)Perl 程序,即使我看不懂它们(不知道 Perl),我想知道你如何能够设法获得如此小的代码来完成需要 20 的代码其他编程语言中的行。
- Perl 的秘密是什么?是否有一种特殊的语法可以让您通过几次击键完成复杂的任务?是正则表达式的混合吗?
我想学习如何编写强大而简短的程序,就像您从这里的代码高尔夫挑战中知道的那样。最好从哪里开始?我不想学习“干净的”Perl - 我想编写脚本,即使一周后我不再理解。
如果还有其他编程语言可以让我编写更短的代码,请告诉我。
I've seen many (code-golf) Perl programs out there and even if I can't read them (Don't know Perl) I wonder how you can manage to get such a small bit of code to do what would take 20 lines in some other programming language.
- What is the secret of Perl? Is there a special syntax that allows you to do complex tasks in few keystrokes? Is it the mix of regular expressions?
I'd like to learn how to write powerful and yet short programs like the ones you know from the code-golf challenges here. What would be the best place to start out? I don't want to learn "clean" Perl - I want to write scripts even I don't understand anymore after a week.
If there are other programming languages out there with which I can write even shorter code, please tell me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有许多因素使 Perl 适合代码高尔夫:
$_
。 (几个动作位于
@_
上。)split
)通常具有默认值让你省略一些参数甚至全部。
<>
。map
和grep
此外,没有限制(默认情况下是关闭的):
undef
变为0
或''
。现在这已经不成问题了,让我明确一点:
高尔夫是一项游戏。
渴望达到让你擅长的 perl-fu 水平是很棒的,但是以
$DIETY
名义打高尔夫球,并非真正的代码。一方面,这是一种可怕的时间浪费。您可能会花一个小时尝试删除一些字符。高尔夫球代码是脆弱的:它几乎总是做出重大假设并且无忧无虑地忽略错误检查。真正的代码不能这么粗心。最后,作为程序员的目标应该是编写清晰、健壮且可维护的代码。编程中有这样一句话:在编写代码时,就好像维护代码的人是一个暴力的反社会者,知道你住在哪里。所以,无论如何,开始打高尔夫球吧;但要意识到这只是玩玩,并以此对待它。
There are a number of factors that make Perl good for code golfing:
$_
if no argument is specified. (A few acton
@_
.)split
) often have defaults thatlet you omit some arguments or even all of them.
<>
.map
andgrep
Additionally, without strictures (which are off be default):
undef
becomes either0
or''
depending on context.Now that that's out of the way, let me be very clear on one point:
Golf is a game.
It's great to aspire to the level of perl-fu that allows you to be good at it, but in the name of
$DIETY
do not golf real code. For one, it's a horrible waste of time. You could spend an hour trying to trim out a few characters. Golfed code is fragile: it almost always makes major assumptions and blithely ignores error checking. Real code can't afford to be so careless. Finally, your goal as a programmer should be to write clear, robust, and maintainable code. There's a saying in programming: Always write your code as if the person who will maintain it is a violent sociopath who knows where you live.So, by all means, start golfing; but realize that it's just playing around and treat it as such.
大多数人都忽略了 Perl 语法和默认运算符的大部分要点。 Perl 在很大程度上是一种“DWIM”(做我的意思)语言。它的主要设计目标之一是“让常见的事情变得简单,让困难的事情成为可能”。
作为其中的一部分,Perl 设计者讨论语法的霍夫曼编码,并思考人们需要做什么,而不是仅仅给他们低级原语。您经常做的事情应该需要最少的打字量,并且函数应该像最常见的行为一样。这样可以节省不少工作量。
例如, split 有许多默认值,因为在某些用例中,不使用某些东西会使用常见情况。如果没有参数,
split
会在空格上分解$_
,因为这是一种非常常见的用法。不太常见但仍然常见的情况是将
$_
分解为其他内容,因此有一个稍长的版本:并且,如果您想明确数据源,您可以指定变量也是如此:
将其视为您通常处理生活的方式。如果您有一个经常执行的常见任务,例如与调酒师交谈,那么您有一个涵盖常见情况的简写:
如果你需要做一些稍微不同的事情,你可以稍微扩展一下:
但你可以随时注意细节
下次您浏览网站时请考虑这一点。您需要点击多少次才能完成常用操作?为什么有些网站易于使用而有些则不然?大多数时候,好的网站要求您做最少的工作来完成常见的事情。与我的银行不同,我的银行需要至少 13 次点击才能完成信用卡账单付款。给他们钱应该很容易。 :)
Most people miss the point of much of Perl's syntax and default operators. Perl is largely a "DWIM" (do what I mean) language. One of it's major design goals is to "make the common things easy and the hard things possible".
As part of that, Perl designers talk about Huffman coding of the syntax and think about what people need to do instead of just giving them low-level primitives. The things that you do often should take the least amount of typing, and functions should act like the most common behavior. This saves quite a bit of work.
For instance, the split has many defaults because there are some use cases where leaving things off uses the common case. With no arguments,
split
breaks up$_
on whitespace because that's a very common use.A bit less common but still frequent case is to break up
$_
on something else, so there's a slightly longer version of that:And, if you wanted to be explicit about the data source, you can specify the variable too:
Think of this as you would normally deal with life. If you have a common task that you perform frequently, like talking to your bartender, you have a shorthand for it the covers the usual case:
If you need to do something, slightly different, you expand that a little:
But you can always note the specifics
Think about this the next time you go through a website. How many clicks does it take for you to do the common operations? Why are some websites easy to use and others not? Most of the time, the good websites require you to do the least amount of work to do the common things. Unlike my bank which requires no fewer than 13 clicks to make a credit card bill payment. It should be really easy to give them money. :)
这并不能回答整个问题,但是关于编写几天之内你将无法阅读的代码,这里有一些语言会鼓励你编写简短的、几乎不可读的代码:
This doesn't answer the whole question, but in regards to writing code you won't be able to read in a couple days, here's a few languages that will encourage you to write short, virtually unreadable code:
Perl 有很多单字符特殊变量,它们提供了很多快捷方式,例如
$.
$_
$@
$/
$1
等。我认为它与内置的正则表达式相结合,允许您编写一些非常简洁但不可读的代码。Perl has a lot of single character special variables that provide a lot of shortcuts eg
$.
$_
$@
$/
$1
etc. I think it's that combined with the built in regular expressions, allows you to write some very concise but unreadable code.Perl 的特殊变量($_、$.、$/等)经常可以使用使代码更短(并且更混乱)。
Perl's special variables ($_, $., $/, etc.) can often be used to make code shorter (and more obfuscated).
我猜想“秘密”在于为经常重复的任务提供本机操作。
在 Perl 最初设想的领域中,您经常必须
而 Perl 提供了简单的运算符来完成这些操作。简短的变量名称和许多事情的默认值的使用只是肉汁。
Perl 也不是第一个走这条路的语言。 Perl 的许多功能或多或少是从 sed 和 awk 以及各种 shell 中原封不动地偷来的(或者经常稍微改进)。对拉里有好处。
当然,perl 并不是最后一个走这条路的,你会在 python、php 和 ruby 中找到类似的功能......人们喜欢结果,并且不会为了获得更常规的语法而放弃它们。
I'd guess that the "secret" is in providing native operations for often repeated tasks.
In the domain that perl was originally envisioned for you often have to
and perl simple provided operators to do these things. The short variable names and use of defaults for many things is just gravy.
Nor was perl the first language to go this way. Many of the features of perl were stolen more-or-less intact (or often slightly improved) from sed and awk and various shells. Good for Larry.
Certainly perl wasn't the last to go this way, you'll find similar features in python and php and ruby and ... People liked the results and weren't about to give them up just to get more regular syntax.
Java 只用一行复制变量而不用担心总线和内存的秘密是什么?答:代码转换为更大的代码。对于曾经发明的每一种语言都是如此。
What's Java's secret of copying a variable in only one line, without worrying about buses and memory? Answer: the code is transformed to bigger code. Same for every language ever invented.