Perl basic syntax


Release date:2023-10-11 Update date:2023-10-21 Editor:admin View counts:318

Label:

Perl basic syntax

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

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

The Perl language does not have a strict format specification, and you can indent it according to your favorite style.

The first perl program

Interactive programming

You can use it on the command line -e option to enter a statement to execute the code, an example is as follows:

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

Enter the above command and enter, and the output result is as follows:

Hello World

Script programming

We put the following code into hello.pl file:

Example

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

In the code /usr/bin/perl is the path to the perl interpreter. To make sure that the file has executable permissions before executing the script, we can change the file permissions to 0755:

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

print can also use parentheses to output a string, and the followingtwo statements output the same result:

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

Script file

Perl code can be written in a text file to .pl.PL as a suffix.

File names can contain numbers, symbols, and letters, but cannot contain spaces, and you can use underscores ( _ ) to replace spaces

A simple Perl file name:

run_oob.pl

Annotation

It is a good programming habit to use comments to make your program readable.

Perl comments are made by using the character # at the beginning of the statement, such as:

# This line is a comment in Perl

Perl also supports multiline comments, and the most common method is to use POD (Plain Old Documentations) for multiline comments. The methods are as follows:

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

Execute the above program, and the output is as follows:

Hello, world

Note:

  • =pod=cut only at the beginning of the line.

  • To = start with =cut the end.

  • = is followed by a character. =cut doesn’t have to do it in the back.

Whitespace in Perl

The Perl interpreter doesn’t care about how many blanks there are, and the following programs work:

Example

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

Execute the above program, and the output is as follows:

Hello, world

But if spaces and branches appear in the string, it will output as is:

Example

#!/usr/bin/perl# Export branchprint"Hello world\\n";

Execute the above program, and the output is as follows:

Hello
          world

All types of blanks such as spaces tab blank lines, etc., if it is outside the quotation marks, the interpreter will ignore it, and if it is inquotation marks, it will be output as is.

Single and double quotation marks

Perl output strings can use single and double quotes, as follows:

Example

#!/usr/bin/perl
print"Hello, world\\n"; # Double quotation marks
print'Hello, world\n';  # Single quotation mark

The output is as follows:

Hello, world
Hello, world\n

As we can see from the results, double quotation marks \\n output line breaks, but single quotation marks do not.

The difference between Perl double quotes and single quotes: double quotes can normally parse some escaped characters and variables, while single quotes cannot be parsed and output 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

Here documents, also known as heredoc, hereis, here- strings, or here- scripts, are a way to define a string in command-line shell (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (such as Perl, PHP, Python, and Ruby).

Overview of usage:

  • 1.It must be followed by a semicolon, otherwise the compilation will not pass.

  • 2.END can be replaced with any other character, as long as the end identifier is consistent with the start identifier.

  • 3.The closing logo must be on a separate line (that is, it must start at the beginning of the line and cannot connect any whitespace and characters before and after).

  • 4.The initial identification can be without quotation marks or single and double quotation marks, and the effect without quotation marks is the same as that with double quotation marks, explaining embedded variables and escape symbols, while single quotation marks do not explain embedded variables and escape symbols.

  • 5.When the content requires embedded quotation marks (single or double quotation marks), there is no need to escape the single and double quotationmarks, which is equivalent to the use of Q and qq.

Example

#/ Usr/bin/perl $a=10$ Var=<"EOF"; This is a Here document instance,
using double quotes.
You can input strings and variables here. For example: a=$aEOFprint
"$var n"$ Var=<'EOF ';
This is a Here document instance, using single quotes.
For example: a=$aEOFprint "$var n";

The output result of executing the above program is:

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

This is a Here document instance, using single quotes.
For 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 ($):

Example

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

The output result of executing the above program is:

Image0

Perl identifier

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

  • Identifier components: English letters (a-z, A-Z)numbers (0-9), and underscores (_).

  • The identifier begins with an English letter or underscore.

  • Identifiers are case sensitive $runoob vs. $Runoob represents two different variables.

Powered by TorCMS (https://github.com/bukun/TorCMS).