Wed 26 Sep 2007
Comparing Python to Perl and Ruby
Posted by Clark Maurer under Productivity, Programming
[61] Comments
Comparing Python to Perl and Ruby
Anyone who knows me well, knows that I am really into programming languages. Like editors, a programming language is a tool which is supposed to make your programming task easier.
Unlike the typical programmer, I have experience designing and implementing languages. Slick-C, the macro language in SlickEdit, is the third fairly major programming language I’ve been involved with. My experience and passion for programming languages gives me quite a bit more insight into performance and elegance.
I started learning Python because SlickEdit needed better support for the language. Unlike Ruby, I never heard a lot of hoopla about Python. The programming world has many, many scripting languages, most of them poorly designed. I figured that Python was probably just another poorly designed language with little or no technical merits.
I’ve been puzzled by the popularity of Perl since it has so many flaws. By the way, just because I dislike some elements of Perl doesn’t mean I will direct SlickEdit not to provide great support for it. SlickEdit language enhancements are based on popularity, and currently Perl is more popular than Python. However, that doesn’t mean I won’t rank on it a bit. Heck, I rank on C/C++ and it’s the language I use most (beware of sharp knife). To date, we have spent WAY more time on Perl and Ruby support than Python. This will always be the case, because it is WAY easier to implement Python support.
I have done my best to make these comparisons unbiased. The only code that I have written for these languages is sample code to better understand these languages. I do have good experience in parsing these languages either for color coding or tagging symbols for SlickEdit. My opinions are based on what I think make a state-of-the-art language. My next article “A Deeper Look at Python”, will talk more about Python and what things make a language state-of-the-art.
This comparison of Python, Perl, and Ruby is from a grammar perspective and does not cover the run-times of these languages. There are often cases where you end up choosing a language due to its run-time features. I’m not going to cover run-time features here. A detailed chart of reasons to choose one or the other which includes run-times would be very useful though.
Python Versus Perl
Python beats Perl by a mile. In a nut shell, Python and Perl have similar language features but Python is way easier to learn and use than Perl. Here are some specific examples:
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | sub func { # You've got to be kidding parameter passing. # Perl 6 intends to fix this. my ($x,$arrayRef)=@_; $i=2; # Crud, this modifies the callers $i. ++$x; ${$arrayRef}[0]=4; #YUCK! Modify the callers array. # By default, all variables are global in Perl. This # is potentially more dangerous. $newvar= 'abc'; # this is a global variable print $x,"\n"; return(1); } $i=1; # Choose the right variable prefix for a list @array= (1, 2); #Passing an array by reference is ugly $x=func(1,\@array); print $newvar,"\n"; print "i=",$i,"\n"; # i is 2 # You're kidding, I need to change the prefix $array[1]=0; print $array[0]," ",$array[1],"\n"; # prints 4 0 |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def func(x,array,z='Cool! Default value'): i=2 # This declares a new local variable x+=1 # Hmm...No ++ operator in Python. array[0]=4; #Modify the callers list print x,z newvar='abc'; # This declares a new local variable return(1) i=1 array= [1, 2] # Python calls this a list # Passing a list by reference works like # the C# List<T> generic. x=func(1,array) #print newvar; This won't compile print "i=",i # i is 1 array[1]=0 print array[0],array[1]; # prints 4 0 |
Python is a big improvement over Perl. I would provide more reasons why Python is much better but I’d like to keep this article short. I will say that Perl has a very large and arguably cryptic grammar. Perl is hard for others to quickly read and understand. I find Perl hard to write as well. Because Perl has a lot of syntax which is so different from other programming languages, you quickly forget it and must read the language documentation again.
Python Versus Ruby
Comparing Python and Ruby is not as simple as showing some small snippets of code as I did above. For me, Python is clearly a better language than Ruby but I can understand why some people might like Ruby better. Ruby took the “kitchen sink” approach. It has language syntax for pretty much all the features of Perl, Python, and then some. Ruby embraces many of Perl’s complex features, which may make Ruby seem more familiar to the Perl fanatic than Python. There are loads of “shorthands” and optional syntax. To the naive person, lots of features give the false impression that the language is better or somehow more powerful.
While more can mean better, it definitely doesn’t in Ruby’s case. Due to the very large grammar, Ruby is way more difficult to learn than Python. The comment on the Ruby web site about Ruby being easy to learn is definitely not true. Reading Ruby code written by someone else is a problem too since others will use constructs you are not familiar with or don’t like. The ambiguities in the language make it much harder for editing tools to provide smart editing features. I’m not willing to give up smart editing features for shorthands which save a few characters. If you are using a dumb editor, then you might see this differently.
In order to keep this article short, I will not show all the constructs in Ruby which make it a large ambiguous grammar. So this may look like I’m nit picking.
Poor Ruby Syntax Choices
Here are some poor Ruby syntax choices:
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 | # Due to these poor choices... if i<5 then puts i end while i<10 then i+=1 end # This syntax for the shorthand forms is hard # to read for most of us. puts i if i<5 i+=1 while i<10 # The syntax below bloats the language. puts i unless i>=5 # Same as "puts i if i<5" |
Python
1 2 3 4 5 6 7 8 | # These long hands are easily compressed if i<5: print i while i<10: i+=1 if i<5: print i while i<10: i+=1 |
Ruby should have chosen a syntax which allows for a more conventional single line statement like one of the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # C-like syntax if ( expr ) { put s; } if ( expr ) put s; # Pascal-like syntax if expr then begin # could use 'do' instead of 'begin' put s; end; if expr then put s; # Python syntax is best if expr: put s if expr: put s |
Function Pointers or Function Objects
Ruby really messed up with function pointers (or function objects):
Ruby
1 2 3 4 5 6 7 8 9 | # What planet did this come from? def func(list) for i in list yield i end end func(['cat','dog','horse']) do |animal| puts animal end |
Python
1 2 3 4 5 6 7 8 9 10 11 | # This is from earth def doSomething(i): print i def func(list,function): for i in list: function(i) # Python does not yet have multi-line anonymous # functions. The lambda functions in Python are weak. func(['cat','dog','horse'],doSomething) |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 | # This is better def func2(list,function) for i in list function.call(i) end end func2(['cat','dog','horse'], # proc(animal) is a better choice # delegate(animal) is like C# proc do |animal| puts animal end ) |
The first Ruby example uses syntax which none of us should ever have to learn. To make matters worse, the keyword “yield” is used which means something totally different in C# and Python. I haven’t yet found a way to pass a predefined function as an argument to a Ruby function. Ruby does support passing method pointers using the “method” feature (ex “variable.method(:methodName)”), but again Python provides a much simpler syntax. To pass a method pointer in Python you just specify “variable.methodName” like you would in C#.
Ruby’s grammar is riddled with multiple ways of doing things. Adding to the confusion, it often unnecessarily chooses syntax different from popular languages we are familiar with. As a result, Ruby is much more difficult to learn, read, and write.
Not everything is better in Python. For example, Ruby’s class/member syntax is better than Python. For me, this doesn’t help Ruby nearly enough to make it a better language.
Summary
Both Ruby and Perl have large ambiguous grammars (Perl’s grammar is worse). They often disambiguate syntax based on context. For example, the slash symbol (/) can mean divide or perform a search depending on the context. In the search case, the slash is used to delimit the string. This is an unnecessary complexity since this is just another way to call a function with string parameters.
There will never be a case where having a very large and ambiguous grammar makes a language better. This is one of the flaws in C++. I’m an excellent C++ programmer and I’ve had to stare at some C++ statements for way too long. Note that Java and C# have very small unambiguous grammars with only a few shorthands. Modern languages have so many features that they must keep the grammar small in order to reduce the learning curve. I guarantee that the best state-of-the-art languages of the future will do the same. For this reason alone, Python is clearly better than Ruby or Perl. Keep in mind that I have not compared the run-times of these languages and there are often cases where you end up choosing a language due to its run-time features.
There are other ways that scripting languages can speed the programming process without creating a large grammar with excessive shorthands. For example, unqualifying names with import/using statements and choosing short name space names for standard run-times (Python does both of these). Also, if the grammar is small and unambiguous, editing tools like SlickEdit can provide more assistance.
My next article “A Deeper Look at Python”, will talk more about Python and what things make a language state-of-the-art.
61 Responses to “ Comparing Python to Perl and Ruby ”
Comments:
Leave a Reply
Trackbacks & Pingbacks:
-
Pingback from | Eugene Ware
September 1st, 2011 at 4:49 am[...] Comparing Python to Perl and Ruby » “Hello World” – The SlickEdit Developer Blog addthis_url = 'http%3A%2F%2Feugeneware.com%2Funcategorized%2Fcomparing-python-to-perl-and-ruby-hello-world-the'; addthis_title = ''; addthis_pub = ''; [...]
-
Trackback from mascota
December 23rd, 2011 at 5:48 ammascota…
[...]Comparing Python to Perl and Ruby » "Hello World" – The SlickEdit Developer Blog[...]…
-
Trackback from book writing
January 4th, 2012 at 3:19 ambook writing…
[...]Comparing Python to Perl and Ruby » "Hello World" – The SlickEdit Developer Blog[...]…
-
Trackback from artsuniversity
January 14th, 2012 at 11:31 amartsuniversity…
[...]Comparing Python to Perl and Ruby » "Hello World" – The SlickEdit Developer Blog[...]…
-
Pingback from Why (some) programming languages are actually designed well … | The Languages
January 20th, 2012 at 10:36 am[...] See a like for like comparison between perl python and ruby [...]
-
Pingback from Why (some) programming languages are actually designed well … | languages
January 20th, 2012 at 11:27 am[...] See a like for like comparison between perl python and ruby [...]
-
Pingback from Taking Another Look At Python
February 4th, 2012 at 9:22 am[...] Ruby shop. Ruby has some incredible strengths, but clarity is not one of them. As Clark Maurer nailed it in his demonstration of a typical Ruby yield statement plus block mess: ”What planet did [...]
September 26th, 2007 at 6:47 pm
come on man, get out of that cave,
“Perl is more popular than Python” it was true 10 years ago. Python is used everywhere, perl – mmmmm for some old projects nobody wants to work with
September 26th, 2007 at 8:17 pm
according to the TIOBE index, python remains lower than perl, but perl has fallen more.
September 27th, 2007 at 12:07 am
From your example I’d say that either you don’t know anything about Perl or you wrote the worst Perl code possible for the sake of your argument. Under those circumstances, I’d refrain to make derogatory comments on the language.
“I don’t know the language, so it’s hard to understand, so it sucks.” That’s all I got from your post. You may have had a valid point later, but I stopped reading.
Regards.
J.
September 27th, 2007 at 3:35 am
Don’t get me wrong, I think Python is a fine language and syntax is part of it, but people get too hung about that silly issue and forget about expressiveness. Program/speak almost any language long enough and you find that syntax is an initial hurdle which is quickly left behind (though some programmers will never be good enough for this). Those new to programming/spoken languages often don’t understand this, so articles which focus on syntax are for beginners. If that’s your target audience, that’s fine, but I hope you’re not falling into the trap of thinking a language is superior merely for syntax.
September 27th, 2007 at 4:06 am
You should learn Perl first.
It is easy to distinguish good Perl program from bad Perl program.
Good program starts with “use strict;”.
Interesting when you modify callers array in Perl it is bad. In Python – it is ok.
You can use prototypes to not pass array with \@array.
${$arrayRef}[0]=4 can be written as $arrayRef->[0]=4, it will be more clear.
New projects in Perl that I can remeber quickly:
Catalyst
Jifty
CommitBit
git
v6
Socialtext Open
WebGUI
dotReader
tidyview
September 27th, 2007 at 5:05 am
This post clearly shows that you have no idea about those languages. Nobody would write Ruby programs that way (they’re ridiculous). Thanks to blocks and closures, loops in Ruby are almost never used. Blocks are also making Ruby code much more succinct than Python’s.
September 27th, 2007 at 6:51 am
oh, and why did you leave out Erlang?? everybody knows it’s the with it next big thing.
September 27th, 2007 at 9:52 am
very good article.
I agree about that Perl is a poor language.
Arguments for that are clear. Main argument against it is: “but there is a way to write Perl stuff the clean way”. Yeah, and that is the case with C/C++ too but it doesn’t help. Perl allows to write bad code and that is the problem: if it’s not restriced, people WILL do it. And YOU, the programmer, have to deal with bad code, even if you yourself write clean code. Programming languages aren’t just about talking to the compiler/interpreter. They are also about talking to other developers. Language shouldn’t allow for ambiguity because then it allows people to misunderstand each other.
September 27th, 2007 at 9:56 am
Thanks mnt, well said.
Thanks Sasha for the comments about Perl. I definitely should have used the -> operator and toned down the arguments for that. I like the “use strict” primitive and I would definitely use it, but I didn’t think others would want to declare everything. From that perspective, the example isn’t a good one. This isn’t the only grammar problem in Perl.
It is true that with Ruby you don’t have to use anonymous functions. They are used quit a bit in the Ruby documentation. I would avoid using them and I would avoid using other constructs as well. This isn’t the only grammar problem in Ruby.
Learning from criticism is very important. If you don’t listen to criticism, you will not be able to improve your software. Most of the time, I’m getting criticism for SlickEdit. I get loads of it. Most of it is pretty good. If I didn’t listen, SlickEdit would not be where it is today.
Perl and Ruby have significant grammar problems. Deal with it. Either improve the language or don’t. I wouldn’t say the same thing if I were to compare the grammars of Java and C#. There is no significant difference which is going to effect productivity. Perl and Ruby have some really impressive technology in them. There’s no way I will argue that they don’t. The grammar is a bit like the user interface of a program. Perl and Ruby need a better user interface.
Like I said, Python wins in grammar. Beginners will clearly benefit and so will intermediate users of the language. The advanced person may not benefit at all. From a customer perspective, why should he/she waste time learning one language if there is another language which does the same thing better?
September 27th, 2007 at 11:29 am
By the way, do you aware of D programming language?
September 27th, 2007 at 1:02 pm
See my response to this “Perl” code.
September 27th, 2007 at 2:05 pm
How can you complain about Ruby syntax when you clearly don’t know it? You missed something very basic:
Also, idiomatic Ruby is as clear to read as Python. It’s a shame you didn’t use it. None of the Perlish syntax is used anymore, and a lot of it will disappear in Ruby 2.0.
September 27th, 2007 at 2:12 pm
About your example with ruby and the using of a block.
You can write your source code with the abbreviation.
['dog', 'cat'].each {|x| puts x }
September 27th, 2007 at 2:17 pm
Mea Culpa: I see that, as an editor developer, you have to support non-idiomatic forms of the languages as long as they exist. Well, at least Ruby will be improving in that respect.
September 27th, 2007 at 2:52 pm
Mark Thomas, of course I knew I could write “if expr then puts s end”. Because I don’t like reading backwards constructs, that is what I would do in my code. I would still need to read the backwards constructs in someone else’s code? If the Perlish syntax goes away, that would be a big plus. Guess we all know what syntax pieces I’m talking about since I never was very specific about it.
mj,
Sadly, I did get more than one Perl programmer to look at the Perl sample code I wrote. I didn’t need anyone else to look at the Ruby or Python samples. Ok, now I’m laughing. See how bad the problem is.
Perl 6 is definitely an improvement. I like the fix in Perl 6 for call by reference parameters. This is the right way for Perl to handle passing arrays by reference. By default, Perl 6 should be strict about syntax. The current way of defining function arguments and passing arrays by reference to a function (\@array) should be disallowed. I would also recommend that variables need to be declared by default. Stronger changes like this would force users to find the right documentation for Perl 6 and help prevent more poorly written code.
September 27th, 2007 at 4:14 pm
From Wikipedia: The code base has been in continuous development since 1994. … New features have been added, yet virtually complete backward compatibility with earlier versions is maintained.
Perl has some problems from the beginning, but has also CPAN, curly braces, rather than indentation/whitespace, fast interpreter, utf8 support and is unix administrators langugage. Perl is my Secret Weapon.
Perl 6 is strict by default.
September 28th, 2007 at 8:14 am
“Perl 6 is strict by default”. Ok, where can I download Perl 6? Oh, I can’t, because it doesn’t exist yet! And wow, it’s 2007 and Perl is finally getting around to not making all variables global by default. And named parameters! Woooohooo, Perl will finally have named parameters! Nevermind that any programming language worth using has had them for decades.
Face it–Perl is an anachronism. Time to move on.
September 28th, 2007 at 8:49 am
I have to agree with some of the previous posters, if you don’t know how to write Perl, don’t compare it to something else.
Perl is my (and lots of other people’s) language of choice for many types of projects. Some people will always whine, bash, and try to be as loud as they can when they hype their own favorite language.
So nothing to see here, …
PS: If you _really_ want to make a comparison, why not ask a Perl developer to convert the example? Well, probably because most of the arguments wouldn’t look that strong then. For example, why does Python do x+=1, but Perl does ++$x? $x += 1 is valid Perl too.
September 28th, 2007 at 9:00 am
@17 , pugs?
September 28th, 2007 at 9:18 am
Ruby doesn’t actually require “then” after if statements.
if i
September 28th, 2007 at 10:07 am
Sorry but the code YOU wrote here is NOT how the “average coder” after spending i.e. 1 year writing ruby, would do.
For example, I dont know of ANYONE who adds a “then” on the same line as if! Your ruby solutions sound a lot more than the python-way to think about the world. Take func() for example – who would do so in ruby? Everyone would use .each instead (or .map if you want to apply it on every member of the array)
Also, in python most i saw will write
if condition:
do something
I DONT see code with one liners in python like you pointed out. Please also note that in python you NEED to use the :
It would be nicer if we could omit the : but this is not possible because of python’s requirement to depend on indent instead (and omit ends, which btw isnt the problem here, indent is somewhat ok, but the ‘:’ is NOT ok)
The other parts are partially correct and partially wrong.
Its a little sad that you chose an all-or-nothing approach.
The part that python’s syntax is SIMPLER is something I would agree. It however depends on the person who writes code too. For example some people use @@class_vars a lot.
I dont, and I didnt in the last years. And I dont miss them at all.
One thing is totally wrong. You claim ruby is NOT easy to learn.
This is WRONG.
Ruby is EASY to learn.
Ruby is HARD to MASTER.
There ARE concepts in Ruby which are harder to understand, so if you dont think you will need them, dont learn them. There is nothing wrong with that, perl doesnt have these (or it has in some obscure way) and you wont need them anyway for most parts.
The key lies in knowing WHEN to use them and when NOT to use them. As Larry once wrote about perl, its key lies in using perl’s strengths not its weakness. Thats a matter of perspective. As far as syntax cleaniness, both ruby and python beat perl.
About real code, the biggest problem is that you omitted some of python’s warts, like the implicit self, which can even be any other name – i know projects which use just a s or r instead of self as naming choice (!)
The best way in GENERAL is to opt for the cleanest possible syntax. In Perl, this is not possible. In Python, it is possible somewhat because it is ENFORCED to remain clean and uniform.
In Ruby, you have a lot more freedom. Example:
cat.meow()
is valid in both ruby and python but
cat.meow
is only valid in ruby, and not in python.
Also, you are wrong if you say perl is more popular than python.
I am a ruby user, but I am 100% sure that python is now more popular than perl (and also, ruby, but this is not because python is better than ruby, its because python had a way better documentation for a long time and was CLEANER than perl)
September 28th, 2007 at 10:12 am
@Anonymous:
Proof?
I can write good Perl and bad Perl, so you seem to be wrong.
Proof?
Don’t confuse popularity with hype and anti-hype.
September 28th, 2007 at 11:13 am
The problem isn’t that you don’t know perl, it’s that you think you do.
For example, python lacks explicit lexical binding: there is no “my” statement, and the analog (added near python2.3) is also assignment. Python also lacks dynamic binding: there is no “local” statement, and there exists no analog whatsoever in python.
Perl can replace functions at compile-time using $::{} syntax. Python can only do it at runtime using eval. Never mind that Perl is already several orders of magnitude faster than python.
So understand that when you promote python as you do, you’re essentially stating that perl programmers should abandon language features and performance in order to have- what is in your opinion- better defaults, and better macros. Never mind that you can’t make your own macros in python.
Maybe after programming for thirty or forty years you’ll have some idea what I’m talking about…
September 28th, 2007 at 12:12 pm
Python is a nice language, but without Ruby’s closures, consistent OO and powerful reflection my choice is done. Readability is a matter of taste, and I prefer Ruby’s (and I don’t regret I’ve switched from Python).
September 28th, 2007 at 12:38 pm
I didn’t cover which of these three scripting languages is faster. However, I did investigate it a bit anyway being the geek that I am.
I keep hearing that Perl is faster than Python. Is this information based on old tests that were run? Was there a different interpreter that you ran? The tests that I have recently run indicate that Python is overall faster than Perl but not by much. Ruby is the slowest because of its mark-and-sweep garbage collector. Some Python loops can cause garbage collections to occur over and over again like Ruby, but this is an uncommon problem. I don’t know if Perl has the same issue.
Python does have one really cool optimization:
s=”";
for i in range(0,100000):
# This often gets very optimized and
# beats the pants off Perl and Ruby
s=s+”a”;
Now that I have mentioned performance, I wouldn’t recommend that anyone make their choice between these languages based on performance.
My article is trying to stress the importance of small, unambiguous, and powerful grammars. Modern languages have so many features that this has become more important. Reduce the grammar and reap the rewards. Of these languages, Python is the only one which achieved that. Yep, it has some flaws, but not that one. Both Ruby and Perl’s grammars are unnecessarily large which is effecting productivity. The exact syntax isn’t the point (familiar syntax can help adoption though).
September 28th, 2007 at 1:38 pm
I’m sorry, but you still didn’t say why it is affecting productivity that much, and why it should affect it in a bad way? I personally feel more of a boost in productivity due to Perl’s expressive nature. Your code is a perfect example for that. Why should Perl optimise something like this
2
3
4
for (0 .. 100_000) {
$str .= 'a';
}
away, when I can write the much cleaner
? Please stop perceiving yourself as markstone of all developers, or as majority when there’s clearly a lot of different opinions. Because this all is nothing more than that: opinions. Saying it ain’t so won’t make it any more true.
September 28th, 2007 at 4:33 pm
http://blog.slickedit.com/?p=162
Agreed 100%, Clark.
Real Perl in the wild, which I have had to modify and maintain, is significantly *worse* than your examples. It’s an unspeakable nightmare of a language; it doesn’t have to be, but Perl programmers lack discipline, even have an active contempt for discipline and readability. I refuse to believe that it is anything but an April Fool’s joke that got out of control, and now Larry Wall is too stubborn to admit the truth. The continuing fiasco of the vaporware Perl 6 should have chased off any remaining users years ago. It’s obvious to anyone who checks their development status that Perl 6 will never be released with half the features claimed, and those new features will be filled with bugs for years.
Ruby is often ugly and confusing, but not nearly as bad as Perl. However, it has serious defects that make it unsuitable for real-world use, IMO: foremost is the lack of Unicode support. That’s right, a Japanese-designed language without Unicode. Madness. The primary application for Ruby, Rails, has even more crippling defects. It can’t be multithreaded (even though Ruby has decent multithreading), and it can’t be used on most existing databases, only new ones made exactly to its pattern.
Python’s simplicity is one of its greatest strengths. There’s one right way to do almost anything, and it’s usually very close to executable pseudocode. The Python libraries (built-in and at are significantly more useful for rapid development than either Perl or Ruby; CPAN is neat, but the tools it gives you are hard to use, while the Python built-in libraries and the Cheese Shop are considerably simpler. The Python interpreter is one of the most useful language shells I’ve ever seen; LISP systems are its only equal, and then you’re dealing with LISP. Python’s development process is also a wonderful thing. The PEP system makes the language well-specified, and driven by the community but filtered by Guido’s common sense and good taste. Python3000, once a collection of hypothetical “wouldn’t it be nice if we had a blank slate” ideas, is now in alpha. It’ll be finished in 2008, long before the supposed “Perl 6″.
September 28th, 2007 at 8:51 pm
There are a lot of commenters complaining that good Perl/Ruby programmers wouldn’t write code like you’ve shown in your article, therefore your conclusions are flawed.
What a lot of these people seem to ignore is that if the language allows the syntax, people will use it. And since you have to be able to understand other people’s code to really be an effective programmer, it is a problem when languages support too many ways to express the same idea. It leads to inconsistency, which reduces readability and maintainability.
September 29th, 2007 at 4:20 pm
I see a lot of comments pointing to the fact that Perl is bad because bad programmers can write bad code in it more easily than in other languages. This is the converse argument to the fact that good programmers can write amazing code in it.
It is the expressiveness of Perl that makes it so powerful and so loved by those who take the time to learn it well.
By tying the hands of programmers so that they can’t write bad code, you eliminate their ability to write extraordinary code. You go down a road that ends with Java; A language pretty much designed to remove the hard task of finding good developers (or even knowing good developers from bad ones during the hiring process) by limiting the damage that the bad programmers can do.
The problem is that this hand tying limits the productivity of good developers, and hides how bad the bad ones are. In my experience (my experience, not a generalization) every Java project I have ever been involved in was way over budget and over time, mired in a morass of over-design and under implementation. To be fair, I have been involved in a few Perl project running the same way. Most however were rousing successes; because we had a team of good developers that used the tool effectively.
I have nothing against Python or Ruby. I think you should use the language that makes you happy if you can, you will always be more productive. But, if your argument is that the selling point of your language is that it sacrifices flexibility, power and expressiveness for the sake of a syntax simple enough for morons to use, you’ve lost me. I have no desire to use the Harrison Bergeron of programming languages (look it up).
On a more personal note, I find your method of demonstrating your point to be indefensible. You either purposefully wrote horrible Perl and Ruby to make the python code look good, in which case you are no better than a snake-oil salesman, or you did it out of ignorance in which case you are not qualified to write on the subject.
At any rate you come off as what we sports fans call a “homer”, defending your pet language without regard for reality.
October 4th, 2007 at 7:25 am
@ Pete: “You go down a road that ends with Java; A language pretty much designed to remove the hard task of finding good developers (or even knowing good developers from bad ones during the hiring process) by limiting the damage that the bad programmers can do.”
Really? I’m sorry that all the Java programmers you’ve worked with are terrible, but I don’t think this is the right way to approach the hiring process.
Thinking you can separate the men from the boys simply because you are using Perl, and the boys will shoot their foot off with unreadable code and bad syntax choices seems like the wrong mentality to me.
Great programmers can write great code in any language…this is one of the fundamental things you learn with a deep Computer Science education. Great programmers know when to use different languages because of their advantages, know how to problem solve, know how to design extensible Object-Oriented systems, know the implementation of data structures, etc.
A programming language is a means to an end, and because Perl has a lot of syntax bloat in it’s grammar does not make it superior to anything else. It still may certainly be the correct language of choice for some tasks.
Clark begins his article by saying: “This comparison of Python, Perl, and Ruby is from a grammar perspective…” That’s all he’s getting at…and all I’m hearing is “He said something bad about Perl, now I’m angry!”
October 4th, 2007 at 1:22 pm
Ugh, another ultimate fighting championship between programming languages.
I found it especially amusing that just about every one of your python code examples would run under ruby with the most minor changes. Or better yet, there’s a better, more concise syntax in Ruby that is either “not good” to you or you didn’t use it at all.
I use both python and ruby and found ruby, at many times, much more intuitive than python. Perhaps that’s because I’m familiar with functional languages (where Ruby borrows much of it’s best practices from).
One thing I simply can’t get is why you don’t like:
…but prefer…
It seems to intuitive to me, I often wish python had the logic in that direction. For example:
rather than
October 5th, 2007 at 9:57 am
In my article, I really didn’t prove that Ruby or Perl was large and ambigous (large being the bigger problem). Doing this would have made my article VERY long. Instead, I pointed out a few things that arguably were poor choices. For Ruby, I pointed out that it would look like I’m nit picking.
Any one extra syntax by itself looks very harmless. Heck, if the only problem with Ruby’s grammar was the addition of backwards ifs and funky delegates, there’s no way I would state that the grammar was large. The problem is that there are tons of ways to do the same thing and/or the chosen syntax is very different for no good reason. In addition, some of the ways are ambiguous and it is very difficult for editing environments to provide as much assistance as they do for other languages. This means the tool support for Ruby will be worse than other languages.
When designing a language, the most important thing is that the grammar be small, powerful, and unambiguous. Next is to choose syntax which is well-known, except when doing so significantly hurts your language. This one can be a nuisance for designers. For example, the C-style “if/while/for” brace blocks are without a doubt the most popular. Python’s “if/for/while” block constructs are clearly better (more readable, no brace styles, more screen realestate). I would recommend choosing a C-style “if/while/for” simply because it is more familiar to most programmers. Also, when you pick popular syntax, you make it easier for existing tools to support your language. I must admit though, if I designed a language just for me, I would choose the Python way.
Most of Ruby’s grammar problems can be corrected by removing syntax from the language without removing features. Grammar aside, the engine in Ruby is totally cool and powerful.
I manage a group of programmers and I’d be afraid to let any of my programmers use Ruby for any project. It’s too easy to write bad code which is hard to maintain.
October 5th, 2007 at 3:30 pm
Ryan, your point is an interesting one, because it points out exactly why I agree with Clark on this particular point:
In this example, I know immediately if I can stop reading. If I’m not out of milk, I can ignore the rest of the statement. For those of us that are accustomed to the short-circuit behavior of C-like languages, _this_ is more intuitive. Why would I want to parse a whole line of logic just to realize a condition way off at the end negates the whole thing?
October 14th, 2007 at 11:04 am
I enjoyed reading your comments. I was searching for articles advancing the use of python over perl when I came upon this site. I have used perl for about 10 years and have in the last few years started using Python for more of my tasks. I do agree that Python has a cleaner syntax and tighter language design rules than perl does. But my reasons for choosing Python for more of what I do lately is for its object-orientedness and that fact that it scales much better for building larger applications with reusable components than Perl does. Still, I am not ready to jettison Perl completely. I will still choose Perl for quick and dirty tasks over Python, especially when it comes to tasks that involve the use of regular expressions. Python still does not have anything that quite compares to the conciseness of:
2
3
4
5
while (<>) {
s/this/that/g;
print;
}
On the other hand, Perl’s OO model really sucks. Which is mostly because OO is an add-on (ad hoc) hack using references, while Python was designed to be object-oriented from the very beginning.
# Perl Person Class
package Person;
#constructor
sub new {
my ($class) = @_;
my $self = {
firstName => undef,
lastName => undef,
ssn => undef,
address => undef
};
bless $self, $class;
return $self;
}
# Python Person
class Person(object):
def __init__(self):
self.firstName = None
self.lastName = None
self.ssn = None
self.address = None
October 21st, 2007 at 10:01 am
Good Perl and bad Perl are two completely different languages which share the same runtime.
The Perl example in the article is so-called Perl 4-style circa 1995, at which time Perl was simply an improvement over sed, awk, shell etc.
Unfortunately, some Perl 4-style programmers never learned how to write nice, tight, strict Perl 5. But the two are really different languages, one quick-and-dirty, the other much more elegant (though maybe not as elegant as others).
Incidentally, doesn’t this discussion about simplicity of syntax inevitably lead us to Lisp? Why not go all the way? Answer: once you learn that syntax, you are more productive. That’s the tradeoff, and yes, years later, some tradeoffs seem like mistakes.
November 8th, 2007 at 11:29 am
Michael: you example written in modern Perl:
package Person;
use Moose;
has ‘firstName’ => (isa => ‘Str’, is => ‘ro’);
has ‘lastName’ => (isa => ‘Str’, is => ‘ro’);
has ‘ssn’ => (isa => ‘Str’, is => ‘rw’);
has ‘address’ => (isa => ‘Str’, is => ‘rw’);
November 20th, 2007 at 4:11 pm
Are you serious? The fact that in python cat.meow references the actual function ‘meow’ and therefore allows you to pass functions around as 1st class objects is so much less expressive than the fact that ruby requires all sorts of voodoo magic to do the same thing.
2
newname()
How do you do that in ruby again? Oh yea…
January 13th, 2008 at 3:45 pm
Very disapponting article, because completely lacks the quality from a person that says “Unlike the typical programmer, I have experience designing and implementing languages.”
The biggest flaw is that this article is not scientific and rigorous at all. The effect is that the article’s value is just a bit more than a pub chat.
First of all, the word “ambiguous” is not appropriate; it has a specific meaning in the grammars, and programming languages can’t be “ambiguous”, otherwise they couldn’t guarantee a correct compile.
Second, there is no such a way as a “better” feature in a language; a scientifical analyses would use much more precise adjectives/attributes.
Third, there is a big ambiguity in this article, which is: do we analyze a language in the context of all the programmers or in its own? It’s not so easy to choose. You are clearly biased towards C-like, but other people like the complete shift from it, and there is no reason to assume that staying Cish is preferrable.
Fourth, and this is the worst, either your knowledge of ruby comes from the news articles, or you didn’t pay attention in reading the Ruby language specifications in some manual.
I am a “typical programmer” and it took me a short time to acknowledge how to pass a function as a parameter.
If you have experience and passion and think of yourself as an excellent [C] programmer, which writes elegant and performant code, well, I would suggest to retire, as your intellectual product is very poor.
April 22nd, 2008 at 10:28 am
Well, I don’t think Ruby _clicked_ for you yet. You’re coding Ruby with a syntax like someone who codes in Pascal. Spend more time with Ruby, the full object abstraction, the concept of message passing instead of function calls, and you will put your creativity too work for solving puzzles in one liners in such a natural way that everything else will just look like bloat.
April 22nd, 2008 at 11:16 am
Also, let me introduce REAL WORLD snippets. That if structure is lame and doesn’t show how Ruby shorthand syntax can simplify and clarify code.
2
3
4
5
6
7
8
9
10
11
:asia => {1750 => 502, 1800 => 632, 1850 => 809},
:europe => {1750 => 163, 1800 => 203, 1850 => 276},
:america => {1750 => 18, 1800 => 31, 1850 => 64}}
world.each_pair do |continent, record|
puts "#{continent.to_s.capitalize}:"
record.each_pair do |year, pop|
puts "\t#{year} - #{pop} " << (">" if pop > 100).to_s
end
end
Which otherwise, would be:
2
3
4
5
6
7
8
9
10
puts "#{continent.to_s.capitalize}:"
record.each_pair do |year, pop|
if pop>100
puts "\t#{year} - #{pop} >"
else
puts "\t#{year} - #{pop}"
end
end
end
Which one is simpler: duplicated strings, or a postfix conditional?
June 4th, 2008 at 6:40 am
Thinking you can separate the men from the boys simply because you are using Perl, and the boys will shoot their foot off with unreadable code and bad syntax choices seems like the wrong mentality to me.
%cscan.info. [ $+ [ %cscan.host ] ] = 1
:Loopa
if (%cscan.cLone == 0) { goto enda }
inc %cscan.cLone.num
if ($istok(%cscan.cLone.nicks,$iaLchan(%cscan.host,%cscan.chan,%cscan.cLone).nick,32) == $true) { dec %cscan.cLone | goto Loopa }
%cscan.cLone.nicks = %cscan.cLone.nicks $iaLchan(%cscan.host,%cscan.chan,%cscan.cLone).nick
dec %cscan.cLone | goto Loopa
:enda
did -a cLone.diaLog 14 $space(3) $iaLchan(%cscan.host,%cscan.chan,0) $space(6) $right(%cscan.host,$caLc($Len(%cscan.host) – 4)) ( $+ %cscan.cLone.nicks $+ )
unset %cscan.cLone.nicks
}
June 28th, 2008 at 9:42 am
I am an industry expert in programming.
Its true.
Perl sucks
Ruby is hopeless
Python rocks.
C++ and C# rules forever – get a life.
June 28th, 2008 at 9:47 am
Perl is clearly the language of choice for those who can’t move forward. Any person who has unbiased experience in spending same time with each language will conclude that, perl has no future and python is beautiful.
July 7th, 2008 at 12:02 pm
Saverio, he’s applying the word ambigous correctly as far as I know. The context-free grammars of these languages are actually ambiguous. This doesn’t mean that the program doesn-t have an unambiguous meaning, it’t just that the ambiguities are resolved by other (contextual) rules.
August 25th, 2008 at 12:58 am
Hello everyone!
Let’s face it!Each one of us have a different opinion on which programming language is better.I think this is going to be a endless topic,and the more I read these stupid posts about ruby x python,java x yoda,the more i think software developers are selfish and the only thing we know how to do is to make fun of someone elses code or programming environment!
I support the language thats fits my actual needs,and so far,ruby has been the choice.May be tomorrow i’ll switch to python or whatever comes up next.
What really matters is the WAY you code it.There are tons of ruby and python codes out there uglier than hell,and if you’re a good programmer,no matter what magic language you work with,your code will always look like
.
crap
Cheers!
August 25th, 2008 at 4:39 pm
your codes are the worst I’ve ever seen.
how a person like you think it can compare programming languages?
Did you want make some money with adsense? I think you’re not even able to do this! You need to humiliate yourself to achieve it..
September 22nd, 2008 at 8:21 pm
Book with Perl/Ruby/Python side-by-side:
http://books.google.com/books?id=v8l72QBDzD0C&printsec=frontcover&dq=cad+scripting+languages&sig=ACfU3U1jLmNSMjZgoV1NdxAgkAblXP6veQ#PPA1,M1
October 11th, 2008 at 4:44 am
re: Speed optimisation.
I pasted in that Python loop you gave Clark (LOL little bit of fun with that!) and gave it a whirl on a couple of machines here.
Apple’s Python (MacOSX Tiger) is blowing chunks with this!! coming in on average around 8 seconds ;-(
Same loop in Apple’s Perl averages in at 0.031 secs. This is 5.8.6 with threads. So tried it with self compiled Perl 5.10 without threads and this comes in faster averaging 0.024s.
Python is spending over 2/3 its time in “sys” so that isn’t good. Somethings is definitely wrong there? ;-(
So did tests on RedHat Enterprise Linux 5. Averages below….
Redhat Python (2.4.3)…. 0.027s
Redhat Perl (5.8.8)…….. 0.009s
Compiled Perl (5.8.8)….. 0.001s
Unfortunately I’m not seeing any Python optimisations over Perl here ;-(
Here’s the equivalent Perl code which you should have no trouble pasting in
2
for my $i (1..100000) { $s.='a' }
/I3az/
October 13th, 2008 at 1:03 pm
Yes, you are right. Perl is WAY faster if you use s.=”a” instead of s=s+”a”. That’s very impressive. All these languages have some awesome technology in them. The programmer’s for all these languages are definitely really good.
March 31st, 2009 at 9:44 am
Actually, $s=$s.’a’ is the perl equivalent to s=s+’a’ and takes the same time as $s.=’a’. ‘.=’ isn’t any more optimized than doing the ‘long’ way.
0m0.018s
0m0.018s
0m0.002s
Incidentally, the python script you posted took 0m0.070s.
April 10th, 2009 at 1:04 pm
The fact that in python cat.meow references the actual function ‘meow’ and therefore allows you to pass functions around as 1st class objects is so much less expressive than the fact that ruby requires all sorts of voodoo magic to do the same thing.
Yes, but then you get warts in Python like this:
2
3
4
5
6
7
self.foo = var
def get_foo(self):
return self.foo
foo = property(get_foo, set_foo)
Where as the same in ruby is accomplished:
attr_accessor :foo
Or you can be explicit with far more intuitive overriding
2
3
@foo = var
end
There’s also the nastiness of decorators which feel like a hack to get around the lack of a rich anonymous functions.
But lastly, my final negative on python is that it’s very inconsistent in design, especially in regard to its OO.
For example, to sort a list in place you do
2
a.sort()
To do a non-inplace sort you do:
2
b = sorted(a)
That’s fine, but if someone learning the language tried to generalize this to a rule, e.g. “in place operations are member functions and non-inplace are not member functions and are appended by ‘ed’”. They’d fail miserably as there is no such consistent language design. For example,
Randomizing a list is in a completely different module and is opposite in effect then sort:
2
3
4
a = [1,2,3,4]
# in place randomize
shuffle(a)
Ruby is much better in this respect. Its design is very consistent though its syntax is very flexible. This leads to a much quicker learning curve as it’s easy to start predicting “the ruby way” from a few basic rules that you can generalize to all object’s/classes behavior.
Python also likes to make a different function for every variation of an operation. For example, for common file and directory operations you must be aware of:
the os module and the shutil module
the following methods:
rmdir
removedirs
remove
unlink
move
copy
cp
Here’s a list of the 12 other modules to get things done with files and directories http://docs.python.org/library/filesys.html
With ruby, they’re in very consolidated and intuitively named modules like “FileUtils”, “File”, and “Dir”.
And lastly, shelling out is a annoying in python. For example,
ruby
Python
2
3
4
cmd1 = ['echo', 'foo']
(output, error) = Popen(cmd1,stdout=subprocess.PIPE).communicate()
Anyone who’s coming from anything but a “what’s unix?” background immediately recognizes which of these is intuitive and which requires a visit to the docs or, more likely, a visit to mailing lists and #python to get a hint as to which module this functionality is hidden in so they read up on the documentation.
Though, even with all its warts and inconsistencies that require frequent trips to the docs (using site specific google search, since python documentation search is horrid), I’m still using python. Mainly due to its deep module library which Ruby does not have (especially in the sciences). Perhaps that’ll change as ruby matures.
Until then, I’ll be warning users about the dangers of using different settings in their editors than the original Python developer or daring to copy/paste code. As well as how to set up your editor to make the annoying indentation errors more obvious.
April 10th, 2009 at 1:05 pm
I have no idea why some of the code ended up as one liners, I just used code blocks.
April 4th, 2011 at 1:59 am
IMOHO this article is opinionated… but…
“FLAME ON!”
Perl is too old, but has the strongest community;
Python is too strict, but has high readability;
Ruby is too slow, but fills the style gap between Perl and Python!
Perl and Python == “Speed”;
Perl and Ruby == “Expressiveness”;
Python and Ruby == “real OOP”;
Either way… You people should hang your heads in shame for foolish bickering and encourage everyone including yourselves to learn all 3 so we can save a few petabytes of storage in the cloud from foolish bickering over personal preferences… when it’s obvious that everyone here likes scripting anyways…
Let’s get back to writing open source code and tearing down Microsoft, Apple, and Oracle.
This silly debate is worse and The Church of Emacs vs. The Cult of Vi. Don’t hate your Nerdy brethren! Respect *nix, Cherish GNU, and Enjoy your wonderful selection of Open Source programming languages… and if you don’t like them make your own, base it off of a existing one. That’s the freedom RMS was/is talking about. These are all great languages with many things to be learned from each of them. Improve yourselves learn more about all of them…
P.S. Just a side note… these Flame wars over which Programming language to learn first really messed with my head when I was first getting started… in fact I just ran away for a while and studied Java because for job purposes it was an obvious choice. To think that you could be confusing a potential programmer and maybe driving them away should be enough to make everyone put down the flame throwers. A little bit of community between these “Communities” could go 1,000 miles for all of us… just think about that before you strike the next match just to watch it burn.
May 21st, 2011 at 2:13 am
And this thread [should] be respectively closed with excellent conclusion by Aimen.