Tag: perl

Perl and Python program syntactic differences

If you know Perl Programming then learning Python is not that hard. But many times we would be confusing Perl syntax for Python and might get into trouble while writing Python code. So here I am listing some differences that I know and will keep adding as I knew it.

Variable names

In Perl we would save a variable like $my_variable/@my_varibale etc, but in Python we just can write the names nothing else needed.

Regex Substituion

In Perl we write $in=~s/to/too/g; In Python it is in = re.sub(r'to', 'too', in). Note the difference? In Perl substituion happens in same variable in Python we explicitly need to specify in which variable we are saving the substitution. To replicate the Perl code we have saved in same variable.


In Perl to import modules we write as use 'modulename' in Python we write as import re. To install module in Perl we have cpan and in Python we use Pip to install modules.

Hash/Dictionary usage

In Perl to assign value to a key we write as $hash{$key} = $value in Python we write as hash[key] = value

Code Indendataion

Perhaps this is the major difference between Python and many other major programming languages. In Perl we have to use curly braces to enclose a block of code into a function/loop/if-else-block in Python we need to indent properly. Also no semicolons are needed to tell the compiler that it is the end of the line we need to just press enter after a line of code. This allows code to look very readable.