Friday, November 22, 2024

A Guide to Programming in Perl, PHP and Python

- Advertisement -

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

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 if you know them very well. But there are so many programming languages in the world. 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 P’s: 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.

- Advertisement -

Perl

Perl is a general-purpose, high-level, dynamic programming language. Currently, Perl 5 is its most popular version, 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 like 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, most of which 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 thing. 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. Huge community support and built-in modules make it very popular.
Popular frameworks. Catalyst, Dancer and Mojolicious are some of the frameworks that use Perl.

Syntax.

Shown below is the basic syntax for Perl:
[stextbox id=”info”]#!/usr/bin/perl
$abc = 10;
print “Value of abc = $abc\n”;
print ‘program is over’;

[/stextbox]

The extension for Perl files is .pl or .pm, which is based on a file or module.

PHP

PHP is also a general-purpose language but it 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 that of 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 version of PHP.

Disadvantages.

    1. Some of the libraries written in PHP are procedural, so it’s difficult for programmers from an object-oriented background.
    2. Since it is open source, security is a major challenge. As the code is available to all, intruders can easily find weaknesses in the code.

  1. It becomes slow after a certain scale. Therefore large-scale organisations use their own customised frameworks to increase performance.
  2. PHP is not suitable for desktop applications. Besides, its error handling capabilities are poor.

Popularity.

PHP is still very popular and 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 end tag:

[stextbox id=”info”]<?php // PHP code ?>[/stextbox]

The file extension is .php, and usually contains PHP scripts and HTML 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, which 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 easier.
  • 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 than older languages like C and C++. It is also an interpreted language.
  • It doesn’t perform multi-processor/multi-core work efficiently.

Popularity.

Python was 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 a 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:

[stextbox id=”info”]

#!/usr/bin/python

print “Hello! Welcome to the world of Python!”[/stextbox]
The extension of the file is .py.

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

There is no specific answer to this question because you 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.

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.

There 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.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 is:
def test(a, b):

[stextbox id=”info”]a[0] = 2
b[0] = 7.5
abc = [0]
xyz = [0]
test(abc, xyz)
abc = abc[0]
xyz = xyz[0][/stextbox]

The Pythonic way of writing the same code is:
def test():

[stextbox id=”info”]

return 2, 7.5
alpha, beta = foo()
Example 2. The non-Pythonic way of writing code is:
i = 0
while i < mylistleng:
do(mylist[i])
i += 1[/stextbox]

The Pythonic way of writing the same code is:

[stextbox id=”info”]for i in range(mylistleng):
do(mylist[i])[/stextbox]

Another Pythonic way of writing the same code is:
for item in mylist:

[stextbox id=”info”]do(item)[/stextbox]

Example 3. A short program for reversing a string is shown below:
[stextbox id=”info”]‘Hello Python’[::-1][/stextbox]


This article first appeared in EFY’s Open Source For You May issue

SHARE YOUR THOUGHTS & COMMENTS

EFY Prime

Unique DIY Projects

Electronics News

Truly Innovative Electronics

Latest DIY Videos

Electronics Components

Electronics Jobs

Calculators For Electronics