Perl 让小代码做这么多事情的秘诀是什么?

发布于 2024-08-28 09:51:12 字数 304 浏览 7 评论 0原文

我见过很多(代码高尔夫)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

抠脚大汉 2024-09-04 09:51:12

有许多因素使 Perl 适合代码高尔夫:

  • 无需数据输入。值可以作为字符串和数字互换使用。
  • “对角线”语法。通常称为 TMTOWTDI(有不止一种方法可以做到这一点。)
  • 默认变量。如果未指定参数,大多数函数都会作用于 $_。 (几个动作
    位于 @_ 上。)
  • 采用多个参数的函数(如 split)通常具有默认值
    让你省略一些参数甚至全部。
  • “神奇的”readline 运算符,<>
  • 高阶函数,如mapgrep
  • 正则表达式集成到语法中(即不是单独的库)
  • 短路运算符返回最后测试的值。
  • 短路运算符可用于流量控制。

此外,没有限制(默认情况下是关闭的):

  • 您不需要声明变量。
  • 裸词自动引用字符串。
  • 根据上下文,undef 变为 0''

现在这已经不成问题了,让我明确一点:

高尔夫是一项游戏。

渴望达到让你擅长的 perl-fu 水平是很棒的,但是以$DIETY 名义打高尔夫球,并非真正的代码。一方面,这是一种可怕的时间浪费。您可能会花一个小时尝试删除一些字符。高尔夫球代码是脆弱的:它几乎总是做出重大假设并且无忧无虑地忽略错误检查。真正的代码不能这么粗心。最后,作为程序员的目标应该是编写清晰、健壮且可维护的代码。编程中有这样一句话:在编写代码时,就好像维护代码的人是一个暴力的反社会者,知道你住在哪里。

所以,无论如何,开始打高尔夫球吧;但要意识到这只是玩玩,并以此对待它。

There are a number of factors that make Perl good for code golfing:

  • No data typing. Values can be used interchangeably as strings and numbers.
  • "Diagonal" syntax. Usually referred to as TMTOWTDI (There's more than one way to do it.)
  • Default variables. Most functions act on $_ if no argument is specified. (A few act
    on @_.)
  • Functions that take multiple arguments (like split) often have defaults that
    let you omit some arguments or even all of them.
  • The "magic" readline operator, <>.
  • Higher order functions like map and grep
  • Regular expressions are integrated into the syntax (i.e. not a separate library)
  • Short-circuiting operators return the last value tested.
  • Short-circuiting operators can be used for flow control.

Additionally, without strictures (which are off be default):

  • You don't need to declare variables.
  • Barewords auto-quote to strings.
  • undef becomes either 0 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.

醉生梦死 2024-09-04 09:51:12

大多数人都忽略了 Perl 语法和默认运算符的大部分要点。 Perl 在很大程度上是一种“DWIM”(做我的意思)语言。它的主要设计目标之一是“让常见的事情变得简单,让困难的事情成为可能”。

作为其中的一部分,Perl 设计者讨论语法的霍夫曼编码,并思考人们需要做什么,而不是仅仅给他们低级原语。您经常做的事情应该需要最少的打字量,并且函数应该像最常见的行为一样。这样可以节省不少工作量。

例如, split 有许多默认值,因为在某些用例中,不使用某些东西会使用常见情况。如果没有参数,split 会在空格上分解 $_,因为这是一种非常常见的用法。

 my @bits = split;

不太常见但仍然常见的情况是将 $_ 分解为其他内容,因此有一个稍长的版本:

 my @bits = split /:/;

并且,如果您想明确数据源,您可以指定变量也是如此:

 my @bits = split /:/, $line;

将其视为您通常处理生活的方式。如果您有一个经常执行的常见任务,例如与调酒师交谈,那么您有一个涵盖常见情况的简写:

平常

如果你需要做一些稍微不同的事情,你可以稍微扩展一下:

通常,但加洋葱

但你可以随时注意细节

肮脏的孟买蓝宝石马提尼摇匀而不搅拌

下次您浏览网站时请考虑这一点。您需要点击多少次才能完成常用操作?为什么有些网站易于使用而有些则不然?大多数时候,好的网站要求您做最少的工作来完成常见的事情。与我的银行不同,我的银行需要至少 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.

 my @bits = split;

A bit less common but still frequent case is to break up $_ on something else, so there's a slightly longer version of that:

 my @bits = split /:/;

And, if you wanted to be explicit about the data source, you can specify the variable too:

 my @bits = split /:/, $line;

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:

The usual

If you need to do something, slightly different, you expand that a little:

The usual, but with onions

But you can always note the specifics

A dirty Bombay Sapphire martini shaken not stirred

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. :)

娇妻 2024-09-04 09:51:12

这并不能回答整个问题,但是关于编写几天之内你将无法阅读的代码,这里有一些语言会鼓励你编写简短的、几乎不可读的代码:

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:

峩卟喜欢 2024-09-04 09:51:12

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.

吃颗糖壮壮胆 2024-09-04 09:51:12

Perl 的特殊变量($_、$.、$/等)经常可以使用使代码更短(并且更混乱)。

Perl's special variables ($_, $., $/, etc.) can often be used to make code shorter (and more obfuscated).

咽泪装欢 2024-09-04 09:51:12

我猜想“秘密”在于为经常重复的任务提供本机操作。

在 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

  • Take input linewise
  • Strip off whitespace
  • Rip lines into words
  • Associate pairs of data
  • ...

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.

猥琐帝 2024-09-04 09:51:12

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文