Comparison of Python and Perl

From Wikiversity
Jump to navigation Jump to search

This original article compares Python and Perl. It is probably somewhat biased in favor of Python, although neutrality is not entirely given up as an objective.

It is sometimes said that in Python there is one way to do things whereas in Perl there are multiple ways of doing things. Whether and to what extent this is true is unclear. Since, in Python, there are at least three ways to do string formatting. Arguably, the main difference between Python and Perl is the overall syntactic and semantic face, not the number of ways in which one can do things. Some say that Python syntax is natural and easy to read in a way in which Perl syntax is not; a formal survey would perhaps be a fine way to properly verify this statement, or a formal scientifically controlled usability measurement experiment.

Syntax[edit | edit source]

There are some striking syntactic and semantic differences between Python and Perl:

  • Python has syntactically sane argument passing resembling on the face of it e.g. Java, like function(argument1, argument2). Perl uses the bizarre syntax like "$arg1, $arg2 = @_;" on a second line after the function start declaration; worse yet, the programmer can refer directly to $1 and $2 (from @_) without ever giving names to the arguments.
  • Python has sane argument passing of lists: one simply passes a list as an argument, and what is passed is a list as a modifiable item. In Perl, list (or "array") is expanded in the way in which its items get mapped to the arguments, one at a time; and to genuinely pass lists, one has to use references.
  • Perl has a dedicated guide to a list of lists, perllol. In Python, nothing is more natural than writing [[1, 2], [3, 4]], with almost no explanation or warning required.
  • Perl does not seem to make a strict difference between types of what it calls scalars in some sense (integers, floats, strings). In Python, while the symbols being bound are not strongly typed, the objects to which symbols are bound are strongly typed. Thus, one can tell the precise type of each object to which a symbol is bound, whether it is an integer, a string, a float, etc.
  • Perl uses a range of symbols (@, $, etc.) and of sequences of these to provide special syntax. Arguably, that makes the code less readable. For instance, in Perl, one writes $#myarray to refer to the length whereas in Python, one writes "len(mylist)". Things get more tedious with the number of these special Perl syntaxes being considerably large.
  • In Perl, each variable name has to be prefixed with with one of $, @, or % (called sigils), which indicates whether it is a scalar, an array or a hash (an associative array, a map, a dictionary). This arguably creates visual noise for the human vision to parse with almost no genuine value (but Perl lovers will differ and claim value). Python does not do that, and nor do C, C++, Java and C#. Furthermore, one has to keep remembering that e.g. @myarray is the way to reference an array whereas $myarrax[0] (rather than @) is the way to reference a scalar member of that array, which is arguably bureaucratic overhead with little or no value.
  • In Perl, conditions for if statements and while statements require brackets around the condition, adding visual noise. Not so in Python.
  • In Perl, one writes e.g. "foreach my $word (@words)" whereas in Python, "for word in words", arguably a little cleaner and more human-friendly.
  • In Python, the logical operators are word-based ("and", "or", etc.) rather than symbol based ("&&", "||", etc.) Thus, in Perl, one writes e.g. "(($x == 20) || (($x > 0) && ($x < 10) && !($x == 5)))" whereas in Python, one writes "(x == 20) or ((x > 0) and (x < 10) and not (x == 5))".
  • Perl uses "last" and "next" for that for which Python uses "break" and "continue" familiar from the C-language family, reducing the memory burden for the common type of programmer who uses multiple languages, e.g. Python and Javascript.
  • Perl statements are required to end in semicolon (";") whereas Python's not, leading to reduction of visual noise in Python source code. However, some will see the semicolon requirement as a plus.
  • Perl has a bizarre syntax for working with classes and objects, unlike Python. Examples shall be provided. One class style for Perl uses the arguably bizarre keyword bless, together with "=>" symbol pair. In Python, member access is exemplified by "object.datamember" and "object.methodcall(arg1, arg2)", unsurprising for a C++ or Java programmer.
  • Python marks code blocks via indentation with no use of "begin", "end" or curly brackets ("{", "}"), whereas Perl marks them using the C-language curly brackets. Therefore, Python forces the programmer to indent the code to ensure proper semantics. Some feel Python's lack of keyword or symbol marking contributes to legibility; some find Python's dependence on indentation too fragile and error-prone.

Further reading:

Performance[edit | edit source]

Python and Perl are in the same performance band or tier, with differences in speed depending on a particular task. By contrast, Java and C# are in a faster, managed-runtime tier, whereas C and C++ are in a yet faster, native-compiled tier.

Further reading:

Libraries[edit | edit source]

Both Perl and Python have extensive permissively (no strong copyleft) licensed libraries, Perl in CPAN and Python in PyPI.

Standard official Python distribution from python.org comes with a solid standard library of modules of functions requiring no installation from PyPI, sometimes referred to as "batteries included". Moreover, there are other Python distributions that include a broader selection of packages preinstalled (requiring no additional installation effort, meaning no manual use of PyPI), such as popular packages for scientific computing. Perl default-installed library seems less rich than Python's, but a proper sourcing and detail is missing.

Further reading:

Popularity[edit | edit source]

Python is much more popular than Perl by multiple metrics, being in the top tier of all languages, sometimes getting to number 1 in some time periods in various indices.

Further reading:

Interactive shell[edit | edit source]

Python comes with a built-in interactive shell, allowing the user to enter one statement at a time and run it as if from a Unix shell console, Windows cmd.exe console or Windows PowerShell. This makes it possible to get a sense or taste of the syntax interactively and try things out. Perl has no such feature by default. In a sense, it is one step further in the direction of the interactivity of an interpreted language as opposed to a compiled language. (We are talking about CPython or PyPy here, both of which allow such interactivity.)

Python interactive shells are also available online (so no installation on a local computer required), e.g. on python.org.w

Further reading:

Trying it online[edit | edit source]

Both Python and Perl can be tried online, e.g. in tio.run.

Further reading:

See also[edit | edit source]

Further reading[edit | edit source]