Easy Tutorial
❮ Perl Operators Home ❯

Perl Basic Syntax

Perl borrows features from C, sed, awk, shell scripting, and many other programming languages. Its syntax is similar to these languages but also has its own characteristics.

A Perl program consists of declarations and statements, which are executed from top to bottom. It includes loops, conditional controls, and each statement ends with a semicolon (;).

Perl does not have strict formatting rules; you can indent according to your preferred style.


First Perl Program

Interactive Programming

You can use the -e option to execute code directly from the command line. Here is an example:

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

After entering the command and pressing Enter, the output is:

Hello World

Script-Based Programming

We can place the following code in a hello.pl file:

Example

#!/usr/bin/perl

# Output "Hello, World"
print "Hello, world\n";

The /usr/bin/perl in the code is the path to the Perl interpreter. Before executing the script, ensure the file has executable permissions. You can change the file permissions to 0755:

$ chmod 0755 hello.pl 
$ ./hello.pl 
Hello, world                   # Output result

The print function can also use parentheses to output strings. The following two statements produce the same result:

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

Script File

Perl code can be written in a text file with the .pl or .PL suffix.

The filename can include numbers, symbols, and letters but cannot contain spaces. You can use underscores (_) to replace spaces.

A simple Perl filename:

run_oob.pl

Comments

Using comments makes your program more readable, which is a good programming practice.

Perl comments are made by starting a line with the # character, like this:

# This line is a comment in Perl

Perl also supports multi-line comments, typically using POD (Plain Old Documentation) for this purpose. Here is how:

Example

#!/usr/bin/perl

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

=pod Comment
This is a multi-line comment
This is a multi-line comment
This is a multi-line comment
This is a multi-line comment
=cut

Executing the above program will produce the output:

Hello, world

Note:


Whitespace in Perl

The Perl interpreter does not care about the amount of whitespace. The following program will run normally:

Example

#!/usr/bin/perl

print       "Hello, world\n";

Executing the above program will produce the output:

Hello, world

However, if whitespace and line breaks appear within a string, they will be output as is:

Example

#!/usr/bin/perl

# Will output line breaks
print "Hello
          world\n";

Executing the above program will produce the output:

Hello
          world

All types of whitespace, such as spaces, tabs, and blank lines, are ignored by the interpreter outside of quotes but are output as is within quotes.


Single and Double Quotes

Perl can output strings using single or double quotes, as shown below:

Example

#!/usr/bin/perl

print "Hello, world\n";    # Double quotes
print 'Hello, world\n';    # Single quotes

The output is as follows:

Hello, world
Hello, world\n

From the results, we can see that the newline character \n in double quotes outputs a line break, while in single quotes it does not.

The difference between double and single quotes in Perl: Double quotes can parse escape characters and variables normally, while single quotes cannot and will output them as is.

Example

#!/usr/bin/perl

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

The output is as follows:

a = 10
a = $a\n

Here Document

A here document, also known as heredoc, hereis, here-string, or here-script, is a method of defining a string in command line shells (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (like Perl, PHP, Python, and Ruby).

Usage overview:

Example

#!/usr/bin/perl

$a = 10;
$var = <<"EOF";
This is a Here document example using double quotes.
You can input strings and variables here.
For example: a = $a
EOF
print "$var\n";

$var = <<'EOF';
This is a Here document example using single quotes.
For example: a = $a
EOF
print "$var\n";

Executing the above program outputs:

This is a Here document example using double quotes.
You can input strings and variables here.
For example: a = 10

This is a Here document example using single quotes.
For example: a = $a

Escape Characters

If we need to output a special character, we can use a backslash () to escape it, for example, to output a dollar sign ($):

Example

#!/usr/bin/perl

$result = "tutorialpro.org \"tutorialpro\"";
print "$result\n";
print "\$result\n";

Executing the above program outputs:


Perl Identifiers

Perl identifiers are names used by the user during programming. Variable names, constant names, function names, block names, etc., used in the program are collectively called identifiers.

❮ Perl Operators Home ❯