The Complete Magazine on Open Source

A guide to start programming in Perl, PHP and Python

5.14K 1

Perl, PHP and Python are three very popular and easy-to-learn programming languages, each with their own advantages and disadvantages. Newbies venturing into the field of programming will find this guide interesting and instructive.

To be a good programmer, mastering one programming language is not enough. In fact, it is very important to understand a customer’s requirements and choose the best available language for the task. It’s easy to shift from one programming language to another language if you know them very well. But there are so many programming languages in the world. Each has its own advantages and disadvantages. By leveraging all the advantages of different languages, a programmer can build a robust application. There are different factors which need to be considered, such as the functionality of the application, support for new features, the platform on which the application will run, performance, security, code size, community and support.
Today, let’s examine the three Ps: Perl, PHP and Python. All these three languages are dynamic and powerful in their own way and can be used in different scenarios, but they are most commonly used to build Web applications and for scripting.

Perl

Perl is a general-purpose, high-level, dynamic programming language. Currently, Perl 5 is the most popular version of it, while the latest version is Perl 6. Perl is derived from different languages like C, AWK, Sed and Shell Script.
Perl is most powerful in string manipulation and regular expressions. It is very useful while parsing huge amounts of data, line by line or based on specific regex. It is also useful for Web development, systems administration, network programming and game development.

The biggest advantage of Perl is that there are many ways to do the same thing. But, sometimes, this advantage creates problems in terms of resulting in non-structural or bad code style.

Advantages

  • Perl is very good at handling regular expressions. Hence, it is preferred when there is a lot of work on regex.
  • Perl code is very short and there is a whole set of one-line code available, which shows the power of Perl.
  • It should be used, depending upon the use case. It can be used as a functional language, sometimes object-oriented, procedural or imperative. How you want to use it depends on you. It handles almost every use case.
  • Perl’s CPAN provides a huge number of modules and tested code, and most of them are completely free.
  • Since Perl is a very old language, it has vast community support.
  • Perl is portable and available for all platforms.

Disadvantages

  • Since Perl’s syntax is complex, sometimes new programmers find it difficult to understand it, because the same code can be written in many different ways. For example, print 11 + 12 + 13 != print (11 + 12) + 13
  • It has poor documentation for object-oriented paradigms compared to Python.
  • In Perl, there are many ways to do the same things. Sometimes, this creates a problem.
  • It is slow compared to other scripting languages.

Popularity

Though Perl has been around for a long time, its growth has been slow compared to some other languages and, yet, it has survived. Huge community support and built-in modules make it very popular.

Popular frameworks

Catalyst, Dancer and Mojolicious are some frameworks that use Perl.

Syntax

Shown below is the basic syntax for Perl. The extension for Perl files is .pl or .pm, which is based on a file or module:

#!/usr/bin/perl
$abc = 10;
print “Value of abc = $abc\n”;
print ‘program is over’;

PHP

PHP is also a general-purpose language but is mainly used for Web development. It’s a server-side scripting language designed by Rasmus Lerdorf. It is very popular for small or medium level website development compared to other languages. It is also used by some giant firms like Facebook, YouTube, and Yahoo! Nowadays, there are a lot of frameworks built on PHP, which compete with Python and Perl. Though demand for Python is increasing, PHP is still very popular in both small and large organisations. The current version of PHP is 7.1.2.

Advantages

  • Most people prefer PHP when it comes to creating dynamic Web pages, because it’s easier to set-up in local host and most of the hosting providers offer built-in PHP support.
  • It’s easy to use. The syntax is mostly the same as the C language. Anyone can easily adopt it.
  • There are lots of frameworks available for PHP and it has immense community support.
  • PHP is stable. Since it’s used by many developers, code maintenance is very quick.
  • PHP has strong database support and provides native session management.
  • PHP 7 is much faster than any other versions of PHP.

Disadvantages

  • Some of the libraries written in PHP are procedural, so it’s difficult for programmers from an object-oriented background.
  • Since it is open source, security is the major challenge as the code is available to all; hence, intruders can easily find weaknesses in the code.
  • It becomes slow after a certain scale. Because of this, large scale organisations use their own customised frameworks to increase performance.
  • PHP is not suitable for desktop applications. Besides, its error handling capabilities are poor.

Popularity

PHP is still very popular and is one of the top-10 languages used by Web developers to create dynamic websites. Web developers should definitely learn PHP to kickstart their careers.

Popular frameworks

Laravel, Symfony, CodeIgniter and Phalcon are some of the popular frameworks.

Syntax

PHP scripting code is written using the <?php start tag and ?> end tag. The file extension is .php, and usually contains PHP scripts and HTML code.

<?php // PHP code ?>

Python

Python is a general-purpose and high-level programming language designed by Guido van Rossum in the late 1980s. It’s very elegant and easy to learn. Nowadays, a beginner’s first priority is to learn Python. It supports multiple paradigms —functional, imperative, procedural and OO (object oriented). Python is an all-round language. Since it’s often a first choice, it can be found everywhere. It can be used in testing, automation, Web development, data analytics, cloud computing, mobile development and game development. Python uses indentation to identify the block, and this eliminates the need for curly brackets and the colon. Python’s philosophy is different, which is, “There is only one way to do the things and it’s called the Pythonic way.” The current version of Python is 3.6.0.

Advantages

  • One of the best things about Python is that it is well documented.
  • Python is used in 3D animation and game development, which differentiates it from other languages.
  • In school, students learn Python quicker than C or C++ because it is easy.
  • Python uses white space indentation, while others use curly braces. Comparatively, Python code is neater and cleaner.
  • Code written in Python is very short compared to other languages, which demonstrates its power.
  • Python also supports JVM so code written in Python can easily work with some of the Java objects/APIs.
  • If you are new to programming, learn Python because by learning this one language, you can easily jump into any field of interest, whether it’s automation, Web development, game development or data analytics. Python is everywhere.

Disadvantages

  • Indentation is the biggest issue in Python. The code will not work and sometimes people get irritated because of it.
  • Python forces programmers to follow a particular convention.
  • It’s slower compared to older languages like C and C++. It is also an interpreted language.
  • It doesn’t perform multi-processor/multi-core work efficiently.

Popularity

Python has been bestowed the TIOBE Programming Language of the Year award in 2007 and 2010. The TIOBE index measures the growth and popularity of the language over the period of one year.

Popular frameworks

Django, TurboGears, Web2py, Bottle, CherryPy, Flask and Pyramid are some of the popular frameworks.

Syntax

Python syntax is very neat and clean. The extension of the file is .py.

#!/usr/bin/python

print "Hello! Welcome to the world of Python!"

So which is the best programming language to learn and use?

There is no specific answer to this question because we can perform almost all tasks with any of the three languages. It depends on what you want to do and how you can do it efficiently. The answer to this question varies based on the applications or usage.

If Web development is the first priority, then PHP is the first preference. For test automation and scripting, Python and Perl are very popular. There are lots of frameworks available in all three languages, and since community support is good, you will never be stuck with a particular problem.

PHP has lots of popular CMS frameworks available like WordPress, Joomla and Drupal. Any newbie can deploy websites easily using these frameworks. There are lots of plugins available for these frameworks and almost all the major functionalities are covered. Similarly, Python has the Django framework, which is very popular and most Python lovers use it for Web development.

My advice would be to learn all three languages and use them based on the situation. Beginners can start with Python as it is very easy, and later jump into learning PHP and Perl, in that order. The basic logic for all the programming languages is the same and the only difference is in terms of the syntax. Usually, the person who knows one language can easily migrate to another language very quickly.

These are never ending discussions on all the forums about which is the best programming language for career growth. My advice would be to learn one language very well. Pick the one you feel comfortable with and slowly venture into exploring the other two.

My preference

I will go with Python as it is evolving very fast and is easy to learn. The use cases of Python cover a very broad range. It is used in almost every IT segment and, by learning this language, you can do wonders in all those segments. So I prefer Python.

The most important thing is to learn the Pythonic way of writing a code. Here are a few examples.

Example 1: The ‘non-Pythonic’ way of writing a code:

def test(a, b):
a[0] = 2
b[0] = 7.5
abc = [0]
xyz = [0]
test(abc, xyz)
abc = abc[0]
xyz = xyz[0]

The Pythonic way of writing the same code:

def test():
return 2, 7.5
alpha, beta = foo()

Example 2: The non-Pythonic way of writing code:

i = 0
while i < mylistleng:
do(mylist[i])
i += 1

The Pythonic way of writing the same code:

for i in range(mylistleng):
do(mylist[i])

Another Pythonic way of writing the same code:

for item in mylist:
do(item)

Example 3: A short program for reversing a string is shown below:

'Hello Python'[::-1]
  • vimuser

    Hi, the article states that Perl is slow. Please explain…slow compared what? Python? Perl is much fast compared to Python.
    The article also states that Perl’s object oriented documentation is not good. Please do state examples.
    The Perl Sigils are quite meaningful once the user understands how to use thwm and when.
    Perl’s regular expression speed blows other similar scripting languages out of water.
    Please provide facts and or analysis while making such statements.