Perl Grammar Quick Check

Source: Internet
Author: User


While learning Perl, Python, and shell scripts can easily confuse their syntax, this article is primarily a personal summary that facilitates the use of queries.

Perl basic syntax, installation, help documentation

File Header format:

# !/usr/bin/perl  Use Strict;  use warnings;

Run the Perl script:

# Call Perl program Perl test. PL # Executable Script chmod 755 Test. PL./

Basic reading and writing:

#Keyboard Input$a= <STDIN>;Chomp($a);Print $a;#file InputOpen(In,"<test.txt") or die "cannot open file:$!\n";Open(Out,">test.txt") or die "cannot open file:$!\n"; while(<IN>){    Chomp; PrintOut"1. $_";  Last;};Close(in);Close(out);

Basic Control Flow:

if () {}elsif() {}else{}
Print " hello! " if (11);  # Shorthand
 while () {    $count+ +;}  for ($i$i$i+ +) {} foreach (@array) {    print$_;}
last;   #  Break next;   # Continue

Data structure: Scalar quantity ($), list (), array (@), hash (%), file handle (<>)

Syntax structure: The statement ends with a semicolon (;), the code block is delimited with curly braces {}, and the dynamic language does not have to indicate the variable type

Single double quotes: single quote q (), all output as-is, double quotation mark QQ (), allow inserting variable

Syntax Conventions: There are many places in Perl where parentheses () are available, especially built-in functions such as print, split, and so on; You can use the increment decrement operator

Install: Windows installed active version, (perl-v) check, Linux pack,

tar -zxvf stable. Tar dir  sh  Configure   make make test         Make Install

Help document: Perldoc command

Perldoc  # will prompt usage perldoc perl  # will show detailed usage perldoc perlfunc  # three major modules Perldoc Perlopperldoc Perlfaq

It is recommended to use a browser to view local HTML documents:

file:///D:/Program%20Files/Perl64/html/index. html

Keywords: special variables

$_: The default parameter, which is the current default action parameter, is important in reading files <FILE>, iterating for (@array), split//, Print

$!: standard error output, usually only with or Die statement

$: A variable within the first parenthesis in the regular expression pattern, such as (. *), (string), and so on

An array of arguments passed by the @_:sub function,

Keyword: string operator

Connection string: The collocated operator (.)

Escaped: backslash \

Distinguish between scalar and string: {} Displays the specified variable name, such as ${name}space

repeating operator (x), such as "-" x 10

Keyword: array, hash

List: Lists are constants, created by parentheses (), separated by commas

# range operator @a = (1.. Ten )@b = (1). Ten .  - )@list= (aa: ZZ);

Create an array: Create an array with a list ()

Referencing arrays: Referencing arrays with square brackets [] subscript

Create hash: Creates a hash with a list (), in order to look good, the key value pairs are organized in the form of = =, or you can create individual

Reference hash: Reference hash with curly braces {}

Keyword: array manipulation

Keywords: (numeric/String) comparison

Numeric comparisons: (= =, >, <, >=, <=,! =)

String comparisons: (eq, GT, lt, ge, le, ne)

Keywords: print usage

Print: Can be directly connected to the variable $var, @array,%hash, the output is no space carriage return, all elements are linked together, an array of a lump, hash a lump, where the hash order is chaotic.

Print: can be double quotation marks, there are spaces between the array, can be distinguished, the hash is invalid, no content, it shows the%hash.

Print: When output to a file, the file handle is in front, the middle is a space (not a comma), followed by the output

Keywords: q (), QQ (), QW (), qx{}

Q (): Enclose the content in single quotation marks

QQ (): Enclose the content in double quotation marks

QW (): A sequence of spaces separated by single quotation marks to assemble an array, shielding the scalar inside

# the two are equivalent, preferably without inline variables @a = QW (abc def GHI)  @b = ('abc','def ','ghi')

qx{}: Capture command output

$directory = ' dir '; $directory _2 = Qx{dir};

Keywords: regular expressions, patterns

Keywords: functions

Standard function Format:

Keywords: common built-in functions

int (5.20);

Length ("Nose");

LC ("ME TOO");

UC ("HAL 9000");

COS (50);

RAND (5)

Keyword: stack operation function

Push ()

Pop ()

Shift (): Remove the last element, common language handler function parameters

Unshift ()

Keywords: command line

Keyword: context

Keyword: chomp

Chomp is an operator that can use parentheses or spaces; it changes the parameter variable, and its return value is 1 or 0

$a="lizhiixn\n";Print $a;Print "next\n";Chomp $a;Print $a;Print "Next";
$b Chomp ($a);  # It's very rarely used .

Perl Grammar Quick Check

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: