Perl vs C/C++
Last Updated :
29 Aug, 2023
Perl is a general purpose, high level interpreted and dynamic programming language. It was developed by Larry Wall, in 1987. There is no official acronym for the Perl, but still, the most used acronym is “Practical Extraction and Reporting Language”. Some of the programmers also refer to Perl as the “Pathologically Eclectic Rubbish Lister” Or “Practically Everything Really Likable”. The acronym “Practical Extraction and Reporting Language” is used widely because Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. It supports both procedural and Object-Oriented programming.
C++ is a general purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. C++ runs on lots of platforms like Windows, Linux, Unix, Mac etc.
Below are some major differences between Perl and c/c++:
Driver function(main()) |
No explicit driver function is required in Perl. |
C/C++ code requires main() function to execute, else code won’t compile. |
Compilation process |
Perl is an interpreted programming language. |
C++ is a general-purpose object-oriented programming (OOP) language. |
Closures |
Perl can use closures with unreachable private data as objects. |
C/C++ doesn’t support closures, where closures can be considered as a function that can be stored as a variable. |
File Extension |
Perl’s scripts are saved using .pl extension. For example perlDocument.pl |
The.c and .cpp file extension is used to save c and c++ code, respectively. Example: myFile.c and myFile.cpp |
Braces |
In Perl, you must put braces around the “then” part of an if statement. Ex: if (condition) {statement;} |
In C/C++ it is not necessary to put braces after if and loops. Ex: if (condition) {statement;} |
string declaration |
Perl uses single quotes to declare string. Use of double quotes force an evaluation of what is inside the string. Example: $x = ‘geeksforgeeks’; |
C/C++ uses double quotes to declare a string. Example: string s =”geeksforgeeks”; |
Comments |
For Inline comments, we use # in Perl. e.g. #Inline-Comment in Perl |
C/C++ uses // for Inline comments. e.g. //Inline-Comment in C/C++. |
Program for addition of two numbers in C++ and Perl
C++
#include <stdio.h>
int add( int x, int y)
{
int res = x + y;
return res;
}
int main()
{
int choice = 3;
int choice2 = 5;
int res = add(choice, choice2);
printf ( "The result is %d" , res);
return 0;
}
|
Perl
#!/usr/bin/perl
$choice = 3;
$choice2 = 5;
$res = add( $choice , $choice2 );
print "The result is $res" ;
sub add
{
( $x , $y ) = @_ ;
$res = $x + $y ;
return $res ;
}
|
Output:
The result is 8
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...