The best Perl basic grammar Tutorial In 2024, In this tutorial you can learn The first perl program,Script files,Note,Perl blank,Single and double quotes,Here Documents,Escape character,Perl identifiers,
Perl borrowed a C, sed, awk, shell scripts, and many of the features of other programming languages, the syntax is somewhat similar with those languages, but also has its own characteristics.
Perl program and a declaration statements, the implementation of the program from top to bottom, including the circulation, control conditions, each statement with a semicolon (;) end.
Perl language is no strict format specification, you can according to their own favorite style indentation.
You can use the command line with the-e option to enter statements to execute the code examples are as follows:
$ perl -e 'print "Hello World\n"'
Enter the above command, press Enter, and output is:
Hello World
We will file the following code intohello.pl:
#!/usr/bin/perl # 输出 "Hello, World" print "Hello, world\n";
Code/ usr / bin / perl is the path to perl interpreter.Before executing the script must first ensure that the file executable permissions, we can first modify the file permissions to 0755:
$ chmod 0755 hello.pl $ ./hello.pl Hello, world # 输出结果
You can also use parentheses to print the output string, the following two statements output the same result:
print("Hello, world\n"); print "Hello, world\n";
perl code can be written in a text file to .pl, .PL as a suffix.
The file name can contain numbers, symbols and letters, but can not contain spaces, you can use an underscore (_) instead of spaces.
A simple Perl File name:
run_oob.pl
Use comments to make your programs readable, it is good programming practice.
perl annotation method in the statement beginning with the # character, such as:
# 这一行是 perl 中的注释
perl also supports multi-line comments, the most common method is to use a POD (Plain Old Documentations) for multi-line comments. Here's how:
#!/usr/bin/perl # 这是一个单行注释 print "Hello, world\n"; =pdo 注释 这是一个多行注释 这是一个多行注释 这是一个多行注释 这是一个多行注释 =cut
The above program, the output is:
Hello, world
note:
- = Pod, = cut only the line.
- = Begin with to = cut ends.
- = To immediately behind a character, = cut back can not.
Perl interpreter does not care how many blank, the following programs can be run properly:
#!/usr/bin/perl print "Hello, world\n";
The above program, the output is:
Hello, world
But if space and branches appear in the string, as he will be output:
#!/usr/bin/perl # 会输出分行 print "Hello world\n";
The above program, the output is:
Hello world
All types of white such as: space, tab, blank lines, etc. If the interpreter will ignore it outside the quotation marks, as if in quotation marks will be output.
perl output string can use single and double quotation marks, as follows:
#!/usr/bin/perl print "Hello, world\n"; # 双引号 print 'Hello, world\n'; # 单引号
The output is as follows:
Hello, world Hello, world\n
From the results we can see that the double quotes \ n a newline is output, but not single quotes.
The difference between Perl double quotes and single quotes: double quotes can normally resolve some of the escape character variable, while the single quotation marks will not be parsed as output.
#!/usr/bin/perl $a = 10; print "a = $a\n"; print 'a = $a\n';
The output is as follows:
a = 10 a = $a\n
Here the document also called heredoc, hereis, here- string or here- script is a command line shell (eg sh, csh, ksh, bash, PowerShell and zsh) and programming languages (such as Perl, PHP, Python and Ruby) method defined in a string.
Overview:
#!/usr/bin/perl $a = 10; $var = <<"EOF"; 这是一个 Here 文档实例,使用双引号。 可以在这输如字符串和变量。 例如:a = $a EOF print "$var\n"; $var = <<'EOF'; 这是一个 Here 文档实例,使用单引号。 例如:a = $a EOF print "$var\n";
The above program output is:
这是一个 Here 文档实例,使用双引号。 可以在这输如字符串和变量。 例如:a = 10 这是一个 Here 文档实例,使用单引号。 例如:a = $a
If we need to output a special character, you can use the backslash (\) to escape, for example, output a dollar sign ($):
#!/usr/bin/perl $result = "本教程 \"w3big\""; print "$result\n"; print "\$result\n";
The above program output is:
Perl identifier is programmed using the user's name, the name of the variable used in the program, the constant names, function names, statements, block name, etc. collectively referred to as an identifier.