perldotcom icon indicating copy to clipboard operation
perldotcom copied to clipboard

Article needed: how Perl is different to Python

Open dnmfarrell opened this issue 7 years ago • 14 comments

We need an article which explains the main differences between Python and Perl in a non-partisan way.

The author should be an experienced Perl and Python programmer!

dnmfarrell avatar Jul 25 '18 01:07 dnmfarrell

Do you have any thoughts about viewpoint? They differ in philosophy, syntax, semantics, and notable use cases - I suspect there's more than one article's worth of material :) . Any particular subtopics of interest for the first article?

(Unfortunately, I do not currently have bandwidth to write an article. However, I am interested in the topic and would like to know so I can keep it in the the back of my head. I am well aware that someone else might beat me to it :) .)

cxw42 avatar Aug 21 '18 10:08 cxw42

I'd really like to see something that promotes Python and the various things we (or anyone) can learn from it.

I'd specifically stay away from code translations or syntax comparisons. This isn't a Rosetta Code article.

The article would appreciate the Zen of Python and how well it serves its users, then compare that to the foundational ideas of Perl and how well that serves its users. Different people and different tasks are better served with different foundational ideas. The same people with different tasks might benefit from different languages.

There are several Python features I wished Perl had (in core):

  • continuations (yield)
  • iterator interfaces for arbitrary objects (I played with that in Object::Iterate)
  • easy arbitrary precision

There are many other features that I like, but that's true of almost any language. There are many features I don't like (but I think that about Perl too).

I also appreciate some of the goals of Python and how it serves their users. Different sorts of programmers need different sorts of ideas to accomplish their task. What can we learn from that?

  • It's easier to know how to do easy tasks where flexibility isn't more important. Different use cases there. Cookbooks are even popular in Perl. Many people just want the answer, not the answerS!
  • I hate the enforced indention, but I tend to like the effect and do it willingly in Perl (without the compilation errors). I understand and accept the goal even if I don't like the implementation.
  • Reading the Python source code is a better experience than reading the perl source code
  • Python's documentation is extremely well organized but squishy in that organization. Perl's docs are very precise but horribly organized (where do you read about variables? Is it perlvar or somewhere else?)

Python also has the idea of "batteries included" where Perl is "minimal to install from CPAN". Both of those have merits and uses.

briandfoy avatar Sep 01 '18 20:09 briandfoy

Someone once asked me

Is Perl like Python?

This was my gloss:

Perl and Python are broadly similar.

The two languages have substantially the same features, and roughly the same performance characteristics. They are used for pretty much the same kind of tasks: they inhabit the same ecological niche in the software world. Both are open-source and non-proprietary.

Both have

  • extensive documentation
  • a large collection of 3rd-party libraries
  • a large and active user base

Both are being actively developed and maintained.

The biggest differences between them are philosophical: Python is screwed down pretty tight, while Perl is very freewheeling. In Python, they say

There should be be one obviously correct way to do each thing,
and everyone should do it that way.

In Perl, we say

There's more than one way to do it.

I've programmed in both languages. I'd rather program in Perl.

swmcd avatar Sep 02 '18 20:09 swmcd

Perl and Python (and Ruby) very much occupy the same niche, but have very different philosophies. I think that except for external factors like library availability and platform support there isn't really a technical reason to prefer one over the other (in other words, if you can reasonably write a solution in one, you can do the same in the other; unlike C where there are clearly tasks where one is much more suitable than the other), there are many more subjective/aesthetic differences.

In that light I think it makes more sense to focus on philosophical differences than on syntactic ones.

It may also be interesting to note that Perl 6 has indeed stolen the nice features from python that Brian mentions.

Leont avatar Sep 02 '18 21:09 Leont

The philosophical differences between Perl and Python are reflected in languages features. Here are two examples.

  1. Indentation One of the most salient--and startling--features of Python is the indentation: indentation is significant, and is used to denote control blocks. People coming from other languages see this and wonder if they've been dropped into FORTRAN purgatory (for their sins). But then they start writing Python, and they pretty quickly get used to it, and find that it's No Big Thing.

But actually, it is a Big Thing. The effect of Python indentation is to make everyone use the same indentation style: you have to, or your code won't work. There's a little bit of wiggle room on the indent size, but overall, Python code has a strikingly uniform appearance across programs and programmers.

In contrast, Perl is (almost completely) whitespace-insensitive, and Perl programmers are free to format their code pretty much as they like.

  1. Auto-vivification Perl auto-vivifies array and hash elements as necessary to satisfy assignments

my %words; $words{foo}{bar} = 42;

Python does not. So you end up with this ubiquitous dance where you check whether a dictionary key exists, and either use it or create it, accordingly

words = {}

if 'foo' in words: words['foo']['bar'] = 42 else: words['foo'] = { 'bar': 42 }

So what are the differing philosophies, or--perhaps more to the point--the differing goals that are served by these design differences? I would say that

  • Perl is designed to maximize the productivity and efficacy of each individual programmer
  • Python is designed to make it easy to manage large programming projects

Python's indentation rules lend Python code a uniform style and appearance. This makes it easier to swap programmers in and out of projects, and that makes it easier to manage those projects.

Python's lack of auto-vivification effectively requires that every data object be explicitly initialized. This does foreclose a certain class of errors, but it does so at a cost of 4x LoC (which, because of the indenting rules, cannot be reduced by removing newlines.)

Perl programmers are certainly free to explicitly initialize their hash elements at the cost of more code, but Perl leaves that trade-off up to each individual programmer. Python makes--and enforces--that decision at the language level. Again, this is attractive to managers who may not trust each programmer on their team to make the right decision.

swmcd avatar Sep 09 '18 17:09 swmcd

We need an article which explains the main differences between Python and Perl in a non-partisan way.

Why Python specifically?

ap avatar Sep 09 '18 20:09 ap

Python is popular and close to Perl.

But, if you'd like to write about a different language comparison we'll take that too!

briandfoy avatar Sep 10 '18 00:09 briandfoy

“Popular and close to Perl” describes about half a dozen languages that everyone here could rattle of immediately. So you leave me to persist. 😊 Why Python specifically?

ap avatar Sep 10 '18 01:09 ap

@ap all of the issues tagged popular-article stem from popular Google search keywords associated with Perl: https://github.com/tpf/perldotcom/projects

dnmfarrell avatar Sep 10 '18 01:09 dnmfarrell

@ap Rattle of that half dozen list! Maybe we can do articles on those too!

briandfoy avatar Sep 10 '18 02:09 briandfoy

@dnmfarrell: Ah. Well, that does make sense. But I find this particular page a more delicate/precarious case than the others. Merely having a “Perl vs Python” page perpetuates that holy war, if that’s the only such page there is. Even if the content itself is kept as objective as possible. I think a less fraught (and actively healthy) framing would be to have a page on how Perl is different from very similar languages and another on how it is similar to some very different languages. But maybe a collection of pages for the benefit of Google after all…

@briandfoy: Ruby, PHP, and Javascript would be the others…

ap avatar Sep 10 '18 07:09 ap

But I find this particular page a more delicate/precarious case than the others. Merely having a “Perl vs Python” page perpetuates that holy war, if that’s the only such page there is. Even if the content itself is kept as objective as possible.

Yeah, I'm rather worried about that as well.

Leont avatar Sep 18 '18 22:09 Leont

Well, so here's a curve ball :) . This demo uses both Perl and Python in the same project! Does anyone else have a dual-language project? Maybe an article showing that they can coexist would reduce the chance of fanning the flames.

Specifics: the demo is a pair of ShaderToy GLSL shaders: one for graphics and one for music. Three Python scripts generate various chunks of GLSL. The shader sources are processed by my/d-ash's Text::PerlPP. The Perl code runs the Python scripts and inlines the results, plus pulls in some includes and runs some embedded Perl code to generate other bits of GLSL (example).

Could I have done all of this in Perl? Certainly.

Did I? No.

Do I remember why? Absolutely not! :tada:

:D

Edit Another example: a project building on prior work in Python, but using Perl for tests. I used MY to hijack EUMM so it will do my make steps, but I can still use make test, prove, or cpanm.

cxw42 avatar Sep 19 '18 00:09 cxw42

Just ran across this short series on moving from Perl to Python. Not much there yet, but I mention it in case it sparks any ideas. (I had not heard of these folks before today.)

cxw42 avatar Dec 04 '18 15:12 cxw42