views:

570

answers:

7

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.

A: 

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.

Coronatus
I wanted to know what in Perl's syntax makes you able to do that
sak
Yeh... that's what I answered.
Coronatus
@Coronatus: Not really. "code is transformed to bigger code" isn't a syntax property.
sepp2k
I mean *what* in Perl's syntax. Not *why*. E.g: Whitespace is optional, there is this construct that lets you do that, etc.
sak
Whitespace is optional in most languages. Python is one of the exceptions to that.
David Dorward
What does this answer even mean? "copying a variable in only one line"? Are you referring to managing memory through garbage collected object references? "worrying about buses..."? Other than writing machine code for some devices, who does? "transformed to bigger code"? In what sense?
MtnViewMark
+8  A: 

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:

Mark Rushakoff
+1 for J. It beats Perl in this respect.
Thomas
+1 more for J. See for example the last program I wrote in J (with explanation):http://stackoverflow.com/questions/2639281/code-golf-the-mandelbrot-set/2643044#2643044
David
+4  A: 

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

eugene y
+7  A: 

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.

Tom
+26  A: 

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.

Michael Carman
Excellent answer. I also have this feeling that Perl's orientation toward lists might be useful for golfing, but I'm not certain and I'm not sure how to phrase it.
FM
Perl has data typing. It just doesn't require the programmer to tell Perl what the types are -- it's assumed that the programmer will keep them straight.
jrockway
Jon almost has it right. Perl is very strongly typed. A scalar is a scalar, and array is an array, and a hash is a hash. You can't change a variable's type. It just doesn't try to further divide what you put in a scalar. It's just a single value. However, Perl does track string, integer, and floating point versions for scalar values behind the scenes.
brian d foy
A: 

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.

dmckee
+6  A: 

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

brian d foy