Menu

PERL TUTORIALS - Perl Syntax Overview

Perl Syntax Overview

ADVERTISEMENTS

First Perl Program

$perl -e 'print "Hello World\n"'

ADVERTISEMENTS

#!/usr/bin/perl

# This will print "Hello, World"
print "Hello, world\n";

ADVERTISEMENTS

print("Hello, world\n");
print "Hello, world\n";

Comments in Perl

# This is a comment in perl

#!/usr/bin/perl

# This is a single line comment
print "Hello, world\n";

=begin comment
This is all part of multiline comment.
You can use as many lines as you like
These comments will be ignored by the 
compiler until the next =cut is encountered.
=cut

Whitespaces in Perl

#!/usr/bin/perl

print       "Hello, world\n";

#!/usr/bin/perl

# This would print with a line break in the middle
print "Hello
          world\n";

Single & Double Quotes in Perl

#!/usr/bin/perl

print "Hello, world\n";
print 'Hello, world\n';

#!/usr/bin/perl

$a = 10;
print "Value of a = $a\n";
print 'Value of a = $a\n';

"Here" Documents

#!/usr/bin/perl

$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be 
interpolated. For example value of a = $a
EOF
print "$var\n";

$var = <<'EOF';
This is case of single quote so variable value will be 
interpolated. For example value of a = $a
EOF
print "$var\n";

Escaping Characters

#!/usr/bin/perl

$result = "This is \"number\"";
print "$result\n";
print "\$result\n";