views:

518

answers:

5

How did Perl gain a reputation (deserved, undeserved, or used to be deserved, no longer so) of being a "write only language"?

Was it

  • The syntax of the language
  • Specific features that were available in the language
  • Specific features not being available in the language (or at least old versions of it)
  • The kind of tasks Perl was being used for
  • The kind of people who use Perl (people who aren't full-time programmers)
  • Criticism from people committed to another language
  • Something else?

Background: I'd like to know if some aspects that gave Perl the reputation of being write-only also apply to other languages (specifically ruby).

Disclaimer: I recognise that Perl doesn't force people to do write-only code (can any language?), and that you can write bad code in any language.

Edit: If reasonable, describe aspects of the language that make it write-only-prone in a way that non-Perl programmers can understand.

+29  A: 

I don't think it really had anything to do with Perl, but the accident of history that Perl became popular in the 1990s.

When the big Internet boom was going on, anyone with a pulse could get a job as a programmer (or a CEO, visionary, whatever). Perl, having a low initial learning curve, was used by a lot of people who didn't really know any more about programming than the very basic Perl they scrounged from examples. They didn't really care about quality or learning programming, but were hoping to cash out of some big internet success. It was the Wild West of programming, and there weren't enough experienced team leaders (if any in most places) to do anything about it. The code they pumped out was horrible, but everything lives forever on the internet. More people copied the crappy examples they saw. A lot of people relied on genetic programming accidently: cutting, pasting, and adjusting until things worked without understanding any of it.

And, at the start of the internet boom, there wasn't any software to do the sorts of things that people wanted to do on their web pages. If they found a script from some 15 year old, they just used it. They'd get some other kid to make some changes, and so on. I'm not making this up, and I'm thinking about very real people whom I won't shame publicly. That stuff got passed around liberally. People built entire business plans around 100 line Perl scripts that they found on the Net, didn't understand, and never intended to rewrite. Some of us have the scars for un-fubaring those situations when they hit the scalability wall (no, you can't have 40,000 subscribers and fork a process for each of them every five minutes).

Perl also has the benefit that it's partially designed to be very accessible. "Regular " people can get quite a bit done with just the stuff in Learning Perl, which is only the very basics and only about 400 pages. Since this part of the audience has very little background with programming, they often come up with interesting paths to their problems. They forget why they cobbled so much weird junk together, but they don't want to change any of it because they won't know how to fix it.

Mind you, this has nothing to do with smarts or intelligence. It's mostly just an experience problem. "Baby Perl" is very easy to use for the inexperienced, or "accidental" programmer. These people typically care more about other tasks than programming, so they don't really have iterative delvelopment and improvement. When they get close to a solution, no matter how ugly, they tend to stop. Unfortunately, that's a lot of the code that the public sees.

Ruby has the advantage of seeing its boom after the dot.bomb, and it started off with a community that mostly knew good programming techniques already and just wanted a better language. I don't think Ruby has had the same problems for that reason. People weren't rushing to mine the internet gold, so Ruby's progress and community evolution has been much more sane. It's not an accident that 37 Signals loves both Ruby and beautiful design. :)

brian d foy
Very good points. If PHP was as old as Perl, it would have gotten the same rep, as it is used today in a very similar way to how Perl was in the 90s.
Ether
How often do we see assertions like "CGI is obsolete--you shouldn't use it 'cause Perl looks line-noise and is too slow."
daotoad
Very good answer +1. Like you said, you can see the "damage" of poorly written code to this day. But as a benefit, we can learn from those mistakes. :)
Nate Shoffner
Actually, Ruby has the same maintainability problems that 1996 Perl did. Programing communities, when confronted with popularity and a lot of new programmers at the same time, tend to make mistakes. Perl learned from those mistakes; it's very likely that you can use any two CPAN modules in the same program. Ruby, on the other hand, has not learned from their mistakes, and monkey patching often precludes using two modules in the same program. Rails is an especially bad example.
jrockway
I don't think Ruby was making the monkeypatching problem because of newbies. I've listened to more than a couple of experienced Ruby programmers talk about how good monkeypatching is.
brian d foy
I didn't know you could do monkeypatching in Perl.
Andrew Grimm
I have a chapter about monkeypatching in _Mastering Perl_. It's a dynamic language, so of course you can do that. :)
brian d foy
Actually I think PHP is getting some of the same rep as Perl now. It's possible to write bad code in any language. Don't worry Ruby you'll get there too. And yes I can point you to awful Python code as well.
xenoterracide
@xenoterracide: Are there some aspects of awfulness that are distinct from language to language?
Andrew Grimm
@Andrew maybe... probably... but I think it's more that there are some awful programmers out there. I'd like to hope I'm not one of them.
xenoterracide
@Andrew and once you've seen enough bad code in a a language you begin to blame the language.
xenoterracide
Very very good explanation. And the following line had me ROFLing: "People built entire business plans around 100 line Perl scripts that they found on the Net, didn't understand, and never intended to rewrite."
j_random_hacker
+17  A: 

A couple of things have contributed to the Perl's write only reputation.
1. As brian noted, popularity in the early dotcom boom.
2. Perl is very flexible and expressive. It gives you more than enough room to hang yourself.
3. Perl has some very unconventional features (sigils, context, list based argument passing, and dynamic scoping, and first class regexes, to name a few) that make it behave differently from other programming languages.
4. Perl is a big, deep language. While you can start toddling around on day one, really understanding the deep details takes time and experience.
5. Backwards compatibility with Perl 4 adds some unusual features.
6. Many bad tutorials exist that promulgate bad or outdated techniques.

Combine all these things and you get a lot of bad code.

The last few years have seen major efforts by the Perl community to emphasize good technique. Tools like Perl::Critic and perltidy help a lot.

I see posts on SO where people expect Perl to act like a shell scripting language. They randomly include files (something that was common under Perl 4), and shell out subprocesses when built-in functions would readily handle the tasks at hand. These posts typically come from people new to Perl, so I surmise that they are somehow finding books or tutorials written in the early to mid-90s that show this stuff.

I've seen some monstrously bad Perl. But I have also seen amazing, stunningly elegant code. The expressiveness that lets a fool soil his bedding also allows wiser heads to write better, cleaner code.

daotoad
Is there any easy way to avoid bad or outdated tutorials?
Andrew Grimm
@Andrew: Probably the best shibboleth is to check for the lines `use strict; use warnings;`. If the tutorial doesn't use them (or at least state a reason for *not* using them), then it's extremely unlikely to be one which promotes good practices. Spelling it "PERL" (in all caps) is also widely considered a bad sign, but I consider that more of a "not involved in the Perl community" flag than necessarily "doesn't know what he's talking about".
Dave Sherohman
@Andrew, to Dave's suggestion for strict and warnings, look for 3 argument open on lexical variables. The sad thing is that a newbie trying to learn doesn't know to look for these things.
daotoad
+2  A: 

All the one-liners that people used to write to automate things! The one-off scripts. The notion of TMTOWTDI.

All those things contribute to perl's line-noise reputation.

Alex
What aspects of Perl make it more prone to one-liners?
Andrew Grimm
Not prone, but suitable. The amount of things that can be compactly expressed in a single line encourages this. (Which isn't necessarily a bad thing)
Alex
@Andrew, Perl has many features to make one-liners easy. Consider `perl -ape '$_ = "@F[1,5]\n"' somefile.log anotherfile.log`. If you don't understand `-a` (autosplit), `-p` (assumed while with print), variable interpolation and array slices, this code isn't going make any sense. It just looks like weird magic. If you do understand Perl, you have a very powerful tool for ad hoc tasks. What is important is that a developer develop a sense of propriety--knowing what techniques are acceptable in a given situation. Abuse these tools at your peril. Use these tools well and gain productivity.
daotoad
@daotoad: So half your program can be in the command-line options? (Technically, it's all in the command-line options, but referring to `-ap`)
Andrew Grimm
Yes, exactly. The perlrun[1] manpage does a pretty good job of describing these. Remember that Perl was originally designed for getting stuff done, fast. Being able to write a chunk of code that was going to be exactly the same as it was the last time via a single character is a huge gain in expressivity - just like a single character in Chinese can express a complex concept. For someone who doesn't know it, Chinese is also pretty opaque. (http://perldoc.perl.org/perlrun.html)
Joe McMahon
+6  A: 

For me, the main reason for Perl being write-only is an enormous amount of ways to do simple things while lacking the most straightforward.

When you read code, you read idioms, and you juxtapose the patterns you see with patterns that are in your head. But if there's a lot of ways to do simple things (say, finding an index of an element in an array) you'll have to think more when you're reading, or keep more patterns in your mind. And these patterns tend to quickly become insanely tricky pieces of code.

There's a question full of solutions how to find an element in an array; some answers link to perlfaq and Perlmonks nodes. Check the number of variants to see what I'm talking about.

Whereas 100% of Ruby users will write array.index(x).

Some people say that they're surprised that the Ruby code they write to solve simple problems is very similar to that others write to solve them. For Perl, each programmer has its own set of favorite idioms (maybe even varying throughout a single script), and codes things differently. Perhaps, that's the reason.

Pavel Shved
I'm not sure what "lacking the most straightforward" means. Can you give an example of where Perl lacks the most straightforward way of doing something?
Kinopiko
He did give an example.
brian d foy
But Perl is not lacking the most straightforward way. In this case, the most straightforward way is to use `grep`.
Kinopiko
You think that's the most straightfoward way, but it's not specifically designed to do that job like .index() is.
brian d foy
I don't know Ruby's index function, so I don't know if it returns a list of values or the first value, but `grep` is a straightforward method of finding elements in a list.
Kinopiko
grep is the wrong way, though. You don't want to scan the entire list once you've found the answer.
brian d foy
Genuine question -- what does Ruby do if there are two or more identical values in the array?
AmbroseChapel
I think that's a bad example. Experienced Perl programmers will use `List::MoreUtils::firstidx` because that's the best tool for the job - most of the other answers cover the case where there duplicate values in the array. Similarly, Ruby programmers will use `Array#index`. Yes, Ruby has the function in core. On the other hand, I've had to hand-roll Ruby functions like `shuffle`, `sum`, `mean` and `permute` (usually monkeypatching `Array`) whereas Perl offers namespaced export of these functions via CPAN.
rjh
@rjh, perhaps it's a bad example (I'm not a good perl developer), but it's just to illustrate my claims, not prove them!
Pavel Shved
@Pavel: I agree with the gist of your argument. Perl usually has one "ideal" way to do something, but it's difficult to teach programmers what those methods are. Hell, we have an entire book (Perl Best Practices) listing the bits of Perl people *shouldn't* use.
rjh
@AmbroseChapel: It finds the first one. To find the last one, use `Array#rindex`. I don't think there's a method to list all indexes where the value matches the desired value.
Andrew Grimm
The linked question has responses to several different implementations depending on the needs of the user. For example, some answers show using `firstidx` to find the first match in an array. Others show how to index the entire array to handle multiple lookups. Some examples explain how to handle duplicated items in the array. I doubt all Ruby programmers would have answered with `Array#index` since I find it hard to believe that no Ruby programmer would see the possible need to make multiple queries without repeated traversing the array.
daotoad
Linking to the FAQ is reasonable, but it's friendly to excerpt and expand on the FAQ article. OTOH, linking to a golf contest isn't exactly reasonable. Golf contests use only the simple metric of code length to measure quality (although devious abuse of obscure features is also appreciated). IMO, it's not a good idea to send a newbie to a golf contest for help. You can learn a lot from well golfed code. But you certainly don't want to emulate the style. The difference be golf and real code must be made clear to newbies. If it's not clear that code golf is a game, then we have a problem.
daotoad
+4  A: 

Perl is heavily influenced by natural languages. There are multiple ways to express code in Perl because there are multiple ways to express things in English.

You can write good English, or you can write bad English. Both get the message across, the former takes more effort on your part - the latter requires more effort on the part of the reader.

Perl allows you to get the job done without any thought for readability. This can be a boon when writing one-liners or one-off scripts. It also allows you to write idiomatic, easily understood code. (If you can get past the sigils, that is.) Perl can scale from one-liners up to "enterprise", depending on how you use it.

Back in the dot com boom a lot of people wrote a lot of bad Perl. But you can write bad code in any language. Perl just makes it easier.

rjh