YouTip LogoYouTip

Perl Syntax

Perl borrows features from C, sed, awk, shell scripts and many other programming languages. Its syntax is somewhat similar to these languages but also has its own characteristics. A Perl program consists of declarations and statements, executed from top to bottom, containing loops, condition controls, and each statement ends with a semicolon. Perl language has no strict formatting rules, you can indent according to your preferred style. * * * ## First Perl Program ### Interactive Programming You can use the **-e** option in the command line to enter statements to execute code, as shown in the following example: $ perl -e 'print "Hello Worldn"' Enter the above command, press Enter, and the output is: Hello World ### Scripting Programming We put the following code into the **hello.pl** file: ## Example #!/usr/bin/perl print"Hello, worldn"; The **/usr/bin/perl** in the code is the path to the perl interpreter. Before executing the script, you need to ensure the file has executable permissions. We can first change the file permissions to 0755: $ chmod 0755 hello.pl $ ./hello.pl Hello, world # Output result print can also use parentheses to output strings. The following two statements output the same result: print("Hello, worldn");print "Hello, worldn"; * * * ## Script Files Perl code can be written in a text file, with .pl or .PL as the suffix. File names can contain numbers, symbols and letters, but cannot contain spaces. You can use underscores (_) instead of spaces. A simple Perl file name: run_oob.pl * * * ## Comments Using comments makes your program easy to read, which 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 print"Hello, worldn"; Execute the above program, and the output is: Hello, world > **Note:** > > > * =pod and =cut can only be at the beginning of a line. > * Start with = and end with =cut. > * = must be immediately followed by a character, =cut does not need to be followed by a character. * * * ## Whitespace in Perl The Perl interpreter does not care how many whitespaces there are. The following program can also run normally: ## Example #!/usr/bin/perl print"Hello, worldn"; Execute the above program, and the output is: Hello, world However, if spaces and line breaks appear inside a string, it will output as is: ## Example #!/usr/bin/perl print"Hello worldn"; Execute the above program, and the output is: Hello world All types of whitespace such as: spaces, tabs, empty lines, etc. will be ignored by the interpreter if outside quotes, but will be output as is if inside quotes. * * * ## Single Quotes and Double Quotes Perl can use single quotes and double quotes to output strings, as shown below: ## Example #!/usr/bin/perl print"Hello, worldn"; print'Hello, worldn'; The output is as follows: Hello, world Hello, worldn From the results, we can see that double quotes n outputs a newline, while single quotes do not. The difference between Perl double quotes and single quotes: Double quotes can normally parse some escape characters and variables, while single quotes cannot parse them and will output as is. ## Example #!/usr/bin/perl$a = 10; print"a = $an"; print'a = $an'; The output is as follows: a = 10 a = $an * * * ## Here Document Here document, also known as heredoc, hereis, here-string or here-script, is a method to define 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: * 1. Must be followed by a semicolon, otherwise the compilation will fail. * 2. EOF can be replaced with any other character, as long as the end identifier matches the start identifier. * 3. The end identifier must be on a separate line at the beginning of the line (i.e., it must start from the beginning of the line, and cannot be connected with any whitespace or character before or after). * 4. The start identifier can be without quotes or with single or double quotes. Without quotes has the same effect as with double quotes, parsing embedded variables and escape symbols. Single quotes will not parse embedded variables and escape symbols. * 5. When the content needs to embed quotes (single or double quotes), no escape character is needed. It automatically escapes single and double quotes, which is similar to the usage of q and qq. ## 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"$varn"; $var = <<'EOF'; This is a Here document example, using single quotes. For example: a = $a EOF print"$varn"; Execute the above program, and the output is: This is a Here document example, using double quotes. You can input strings and variables here. For example: a = 10This 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 the dollar sign ($): ## Example #!/usr/bin/perl$result = " "tutorial""; print"$resultn"; print"$resultn"; Execute the above program, and the output is: !(#) * * * ## Perl Identifiers Perl identifiers are the names used by users when programming. Variable names, constant names, function names, statement block names, etc. used in programs are all called identifiers. * Identifier components: English letters (a~z, A~Z), numbers (0~9) and underscores (_). * Identifiers start with an English letter or underscore. * Identifiers are case-sensitive. $tutorial and $Tutorial represent two different variables.
← Perl ArraysPhp7 Mongdb Tutorial β†’