📜  Perl与C / C++

📅  最后修改于: 2021-05-30 03:25:59             🧑  作者: Mango

Perl是一种通用的高级解释和动态编程语言。它由拉里·沃尔(Larry Wall)在1987年开发。Perl没有正式的缩写,但是,最常用的缩写是“ Practical Extraction and Reporting Language”。一些程序员还称Perl为“病态折衷的垃圾清单”或“实际上一切都是宜人的”。首字母缩写词“ Practical Extraction and Reporting Language”被广泛使用,因为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 repectively. 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等的更多准备工作,请参阅“完整面试准备课程”