Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Perl's secret of getting small code do so much?

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.

like image 356
sak Avatar asked Apr 11 '10 12:04

sak


People also ask

Does anyone still use Perl?

In 2020, it's easy to write off Perl as a language whose heyday has passed. But the reality is that, more than thirty years after its first release, Perl remains not only alive but still evolving (hello Perl 7!).

What happened to Perl programming?

In bioinformatics, where Perl's position as the most popular scripting language powered many 1990s breakthroughs like genetic sequencing, Perl has been supplanted by Python and the statistical language R (a variant of S-plus and descendent of S, also developed in the 1980s).

Is Python inspired from Perl?

The Python applications are being rewritten in Java because of maintenance issues (mainly caused by lack of proper typing). Python does not rule here. You might want to try Ruby for some of those things you reached to Python for. It takes direct inspiration from Perl and does a lot of those things better, IMO.

Is Perl popular?

Perl dropped to an all-time low on the most recent TIOBE Index that ranked the 20 most popular programming languages.


1 Answers

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.

like image 101
Michael Carman Avatar answered Nov 16 '22 01:11

Michael Carman