📜  Perl 与 C/C++

📅  最后修改于: 2021-09-14 02:43:43             🧑  作者: Mango

Perl 是一种通用的、高级的解释型和动态编程语言。它由 Larry Wall 于 1987 年开发。 Perl 没有官方首字母缩略词,但最常用的首字母缩略词仍然是“实用提取和报告语言”。一些程序员还将 Perl 称为“病态折衷的垃圾列表”或“几乎所有东西都非常讨人喜欢”。首字母缩略词“实用提取和报告语言”被广泛使用,因为 Perl 最初是为文本处理而开发的,例如从指定的文本文件中提取所需的信息并将文本文件转换为不同的形式。它支持面向过程和面向对象的编程。
C++ 是一种通用编程语言,现在广泛用于竞争性编程。它具有命令式、面向对象和通用编程功能。 C++ 可以在很多平台上运行,比如 Windows、Linux、Unix、Mac 等。
下面是 Perl 和 c/c++ 之间的一些主要区别:

Feature Perl 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 function that can be stored as a variable.
File Extension Perl 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 lopps. 
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++. 
 

C++和Perl中两个数相加的程序

C++
// C++ program to add two numbers
#include 
 
// Function to perform addition
// operation
int add(int x, int y)
{
    int res = x + y;
    return res;
}
 
// Driver Code
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
 
# Perl program to add two numbers
$choice = 3;
$choice2 = 5;
$res = add($choice, $choice2);
print "The result is $res";
 
# Subroutine to perform
# addition operation
sub add
{
    ($x, $y) = @_;
    $res = $x + $y;
    return $res;
}


输出:

The result is 8
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程