Skip to content

Instantly share code, notes, and snippets.

@run4flat
Last active December 17, 2015 07:59
Show Gist options
  • Save run4flat/5576943 to your computer and use it in GitHub Desktop.
Save run4flat/5576943 to your computer and use it in GitHub Desktop.
tentative feedback to reddit

djimbob, c'mon. FUDDING on Perl 4 is hardly helpful for the OP. If you're going to argue Python against Perl, you should argue Python 3 vs Perl 5.16. This is 2013, after all.

Python is a modern well-designed multi-paradigm programming language.

Sorta. Of course, Python has its warts, like join being a string method and not a list method, and reverse working in-place instead of returning a new list. And, Ruby and Perl are also well-designed multi-paradigm programming languages, and they have their warts, too. As far as a scientist is concerned, Perl and Python differentiate from Ruby because they have N-dimensional libraries, PDL and numpy, respectively, that make them as performant as Matlab (for 99.9% of uses, that is). If the OP is looking for a scripting glue language, he/she should give careful thought to Python or Perl. (Some will also throw R or Matlab into the mix, but I don't think they're well designed languages, just popular ones.)

Python code reads beautifully.

Nope. Check out (the most up-voted solution to reversing the words in a string)[http://stackoverflow.com/questions/12336105/python-reverse-list] from StackOverflow. On a line-by-line basis, Python code tends to read well, but so too does a great deal of the Perl code that I come across these days. Another example: this documentation example for an optimization routine from scipy. Also, because Python's blocks are based on indentation, I've seen a lot of code that didn't use vertical whitespace. Let me tell you, that is far from beautiful.

Perl is a shell script on steroids. It's code appears hackish. Perl is often mocked as a write-only language.

I think you meant to say, "My use of Perl is as if it were a shell script on steroids. My Perl code appears hackish and is mocked as write-only." I'm sorry that your Perl code was mocked, but it sounds like you never actually learned to use it as a programming language. I have had nothing but good and professional interactions with fellow Perl programmers.

By the way, there's nothing wrong with your usage of Perl. The language and its use scale very well from shell one-liners (which you can't do in Python) to projects with millions of lines of code (see, for example, all of CPAN). In fact, the Perl community won't shun you if you use Perl just for scripting. But, if you don't know what you're talking about, perhaps you should be a little less vocal against Perl. Perhaps it would be wiser to just say why you think Python is a good language for the OP. All of that said, if you want to become a better Perl programmer in particular and a better programmer in general, there's a fantastic book written by one of the best programming communicators of our time called Perl Best Practices. A few sections are a bit dated, but that's OK, it's a wonderful next step for a budding Perl programmer.

It encourages there being multiple ways of writing anything

As indeed, there always are, even in Python. The difference is that Perl encourages its practitioners to actually think about how the many ways they could write something, and then choose the one that best expresses their intent.

and this feature makes building complex code near impossible to read (and hence write).

Nothing could be farther from the truth. There is always more than one way to architect a solution to a complex problem. "There is more than one way to do it," one of Perl's mottos, trains its practitioners from the very beginning to weigh their options and consider design alternatives. The many ways of expressing something means I can usually be more expressive, and write more comprehensible code, in Perl than I can in Python.

Take this perl advocates examples. ... Take the equivalent python example:

Whoah, hold on. You're pulling an example from the millennium, sir. That's 13 years ago. And, your Python code doesn't actually do what the Perl code does. (You didn't even test to make sure that your Python code did what it was supposed to do?) The Perl community has moved passed this terse way of expressing such code. If I were to write this, I would say it thus:

use 5.12.0;
while(my $line = <>) {
    chomp $line;
    my @columns = split /:/, $line;
    say join(':', reverse @columns);
}

Now, aren't you glad there's more than one way to do this in Perl? (I'll get to the use of <> in a second.) Believe it or not, in Python, there's also more than one way to do this. One way, based on an enthusiastically appreciated expression found on Stack Overflow, looks like this:

with open('test.txt') as f:
    for row in f:
        print ':'.join(row.strip().split(':')[::-1])

Call me crazy, but I don't think that's beautiful. Indeed, I think that's just ugly. A slightly more legible example that more closely mimics the Perl is this:

with open('test.txt') as f:
    for row in f:
        columns = reverse(row.strip().split(':'))
        columns.reverse()
        print ':'.join(columns)

But I still don't see how that's a win for Python. At best, it's a wash, and it's not as functional as the Perl code. The Perl example will work in a shell pipeline, or process multiple filenames given on the command line, all thanks to some well-documented magic with how the <> operator works (which is why I kept it in). The Python code is not nearly so versatile: the filename is hard coded or you use even more lines to manually loop over multiple filenames from sys.argv, or a conditional to read from stdin instead. Also, the Perl example easily scales to more complex column separators, since it uses a regex and not a string. And in Perl, regexes are cool, whereas in Python they're advanced.

If you actually take a little bit of time to learn the language, you will find that it usually Does What You Mean. For example:

# Python
print "2/5 is", 2/5, "and 5/2 is", 5/2
# prints "2/5 is 0 and 5/2 is 2"

# Perl
say "2/5 is ", 2/5, " and 5/2 is ", 5/2;
# prints "2/5 is 0.2 and 5/2 is 2.5"

Perl does the right thing here. Python does not. I can't tell you how many times I've been bitten by Python's divide-by-integer stupidity. It's enough to make me add even more punctuation to my Python code, just to be defensive.

If the OP is looking for a language that is fun and flexible, with lots of powerful extensions freely available, Perl is a great solution. It worked for me when I wrote my dissertation, and I'm sure it'll work for him, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment