C++ vs. Perl



C++ Perl Comments
No comparison Begin every program with the shebang:
On our system: #!/usr/bin/perl
The shebang points to where perl is on the system. Most systems have it linked to /usr/local/bin/perl or /usr/bin/perl.
#include <foo> use foo; There is also a Perl statement require, which is similar, but not the same.
Preferred usage today is use.
int main() No explicit main program in Perl.
int x = 5;
float x = 5;
double x = 5;
$x = 5; Perl treats almost every number internally as a double precision float.
String x = "meow"; $x = 'meow'; The difference between single and double quotes on strings is significant.
Double quotes force an evaluation of what is inside the string.
Only use double quotes when you NEED to evaluate the contents on the string.
This will be further explained in class.
if ( condition )
statement;
if ( condition )
{ statement; }
In Perl, you MUST put braces around the "then" part of an if statement.
while ( condition)
statement;
while ( condition )
{ statement; }
In Perl, you MUST put braces around the statements.
cin>>x; $x = <STDIN>; Since STDIN is typically terminated with a \n,
make sure to chomp your input (chomp $x;).
Comparison ops:
== != < > <= >=
For numbers: == != < > <= >=
For strings: eq ne lt gt le ge
cout<<"Hello"<<endl;
cout<<x;
print "Hello\n";
print $x;

Note: I sometimes use Java instead of C++ syntax for ease.