Perl basic syntax

Perl borrows features from C, sed, awk, shell scripts, and many other programming languages. The syntax is somewhat similar to these languages and has its own characteristics.

Perl programs consist of declarations and statements. The program executes from top to bottom, including loops, conditional controls, and each statement ends with a semicolon (;).

The Perl language doesn't have strict formatting specifications, you can indent it according to your favorite style.

The first perl software

Interactive programming

You can use the -e option on the command line to enter statements to execute the code, as shown in the following example:

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

Enter the above command, after the carriage return, the output is:

Hello World

Scripting Programming

We put the following code in the hello.pl file:


#!/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, make sure the file has executable permissions. We can first change the file permissions to 0755 :

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

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

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

Script file

The perl code can be written in a text file with a .pl, .PL suffix.

File names can contain numbers, symbols, and letters, but they cannot contain spaces. You can use an underscore (_) instead of a space.

A simple Perl file name:



Using comments makes your program easy to read, which is a good programming practice.

The method of perl annotation is to use the character # at the beginning of the statement, such as:

# This line is a comment in perl

perl also supports multi-line comments. The most common method is to use POD (Plain Old Documentations) for multi-line comments. Methods as below:


#!/usr/bin/perl # This is a one-line comment print "Hello, world\n"; =pod Comments 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

Execute the above program, the output is:

Hello, world


  • =pod, =cut can only be at the beginning of the line.
  • Start with = and end with =cut.
  • =There is a character immediately after it, and =cut can be used later.

Blank in Perl

The Perl interpreter won't care how many blanks there are, and the following programs will work fine:


#!/usr/bin/perl print "Hello, world\n";

Execute the above program, the output is:

Hello, world

But if spaces and lines appear in the string, they will output as-is:


#!/usr/bin/perl # will output a branch print "Hello World\n";

Execute the above program, the output is:


All types of blanks such as: spaces, tabs, blank lines, etc. If the quoter ignores it outside the quotes, it will be output as it is inside the quotes.

single and double quotes

The perl output string can be in single and double quotes as follows:


#!/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 double quotes \n output a newline, and the single quotes are not.

The difference between Perl double quotes and single quotes: Double quotes can parse some escape characters and variables normally, and single quotes cannot be parsed and will be output as they are.


#!/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

Here documentation, also known as heredoc, hereis, here-string, or here-script, is a command-line shell (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages ​​(like Perl, PHP). , Python and Ruby) define a string method.

Usage overview:

  • 1. Must be followed by a semicolon, otherwise the compilation will not pass.
  • 2. END can be replaced with any other characters, just ensure that the end tag is consistent with the start tag.
  • 3. The end mark must be on the top of the line alone (that is, it must start from the beginning of the line, and can not connect any blanks and characters before and after).
  • 4. Start identification can be without quotes or with single and double quotes, without quotes and double quotes, explain embedded variables and escape symbols, with single quotes does not explain embedded variables and escape symbols.
  • 5. When the content needs embedded quotation marks (single or double quotation marks), there is no need to add escape characters, and the essays are escaped by single and double quotation marks, which is quite similar to the usage of q and qq.


#!/usr/bin/perl $a= 10 ; $var = <<<" EOF"; This is a Here document instance with double quotes. You can lose strings and variables here. For example:a = $a EOF print "$var\n"; $var = <<<' EOF'; This is a Here document instance, using single quotes. For example:a = $a EOF print "$var\n";

Execute the above program output:

This is a  Here Document instance, using double quotes. 
You can lose strings and variables here. 
Example:a = 10

This is a  Here Document instance, using single quotes. 
Example:a = $a

escape character

If we need to output a special character, we can escape it with a backslash (\), such as the output dollar sign ($):


#!/usr/bin/perl $result = " Novice Tutorial\"Welookups\""; print "$result\n"; print "\$result\n";

Perl identifier

The Perl identifier is the name used by the user for programming. The variable names, constant names, function names, and statement block names used in the program are collectively referred to as identifiers.

  • The identifier consists of English letters (a~z, A~Z), numbers (0~9) and underscores (_).
  • The identifier begins with an English letter or an underscore.
  • Identifiers are case sensitive, and $Welookups and $Welookups represent two different variables.

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.