Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Basic Syntax

Perl Basic Syntax

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

A Perl program consists of declarations and statements. The program executes from top to bottom, including loops and conditional control, and each statement ends with a semicolon ;.

The Perl language has no strict formatting rules; you can indent according to your preferred style.


The first perl program

Interactive programming

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



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

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



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. Before executing the script, make sure the file has executable permission; we can first change the file permission to 0755:



$ chmod 0755 hello.pl 

$ ./hello.pl 

Hello, world                   # 输出结果

print 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 .pl or .PL as the extension.

A file name can contain numbers, symbols, and letters, but not spaces; you can use an underscore (_) to replace spaces.

A simple Perl file name:

run_oob.pl


Notes

Using comments to make your program easy to read is a good programming habit.

The method for perl comments is to use the character # at the beginning of a statement, for example:



# 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. The method is 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

Running the above program, the output is:



Hello, world

Note:

  • =pod and =cut can only be at the beginning of a line.
  • Start with = and end with =cut.
  • A character must immediately follow =, but nothing needs to follow =cut.

  • Whitespace in Perl

    The Perl interpreter does not care how much whitespace there is; the following program also runs normally:

    Example

    #!/usr/bin/perl

    print

    "

    Hello, world

    \n

    "

    ;

    Running the above program, the output is:

    
    
    Hello, world
    
    

    However, if spaces and line breaks appear inside a string, they will be output as-is:

    Example

    #!/usr/bin/perl

    Line breaks will be output

    print

    "

    Hello world

    \n

    "

    ;

    Running the above program, the output is:

    
    
    Hello
    
              world
    
    

    All types of whitespace, such as spaces, tab, blank lines, etc., are ignored by the interpreter if outside quotes, and output as-is if inside quotes.


    Single Quotes and Double Quotes

    perl can output strings using single quotes and double quotes, as shown below:

    Example

    #!/usr/bin/perl

    print

    "

    Hello, world

    \n

    "

    ;

    Double quote

    print

    '

    Hello, world\n

    '

    ;

    Single quote

    The output is as follows:

    
    
    Hello, world
    
    Hello, world\n
    
    

    From the result we can see that double quotes \n output a newline, while single quotes do not.

    The difference between double quotes and single quotes in Perl: double quotes can normally parse some escape characters and variables, while single quotes cannot parse them and 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 is also called heredoc, hereis, here-string, or here-script; it is a method of defining a string in command-line shells (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (such as 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 enter 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

    "

    ;

    The output of the above program is:

    
    
    这是一个 Here 文档实例,使用双引号。
    
    可以在这输入字符串和变量。
    
    例如:a = 10
    
    
    
    这是一个 Here 文档实例,使用单引号。
    
    例如:a = $a
    
    

    Escape character

    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

    =

    "

    Runoob Tutorial

    "

    runoob

    "

    "

    ;

    print

    "

    $result

    \n

    "

    ;

    print

    "

    $

    result

    \n

    "

    ;

    The output of the above program is:


    Perl Identifiers

    Perl identifiers are names used by users when programming; variable names, constant names, function names, statement block names, etc., used in a program are collectively called identifiers.