Introduction to the Perl language instructions for a scalar scalar constant numeric string number and string conversion scalar variable Print output result comparison operator get user input undef value scalar context two list array list arrays note Operator foreach statement list context three Associative array creation add modified data value Delete Four subroutine definition call return value formal parameter private variable my state five statement
Getting started with the Perl language
Script-style start #!/bin/perl
Script-style Comments: #
Often the hint that the variable is not used or appears only once means that the name of the variable may be incorrectly written, especially in languages that do not need to be declared. Preface
Project needs, this article refer to the <perl language introduction > main record some of the high-level languages such as C + + and other places, Quick Start Perl
Compiling Instructions
1.use Strict: Tell the compiler that the next code should be rigorous, followed by a good programming style, such as requiring that variables be declared before they are used
2.use warnning: Use warnings to play log one, scalar $ for non-rigorous places
The simplest data type, number or string
Scalar Constants 1. Digital
Perl is a double-precision store of numbers, without integers, even integers are converted to double-precision.
Support for the easy-to-read effect of dividing numbers by underscores
With two * * 2. String
The characters in single quotation marks are the characters themselves
The string in double quotes may be a newline character or something.
string concatenation. For example: ' A '. ' B ' is equivalent to ' AB '
String repetition operator X, ' a ' x4 equivalent to ' aaaa ', 5x4 equivalent to ' 5555 ' 3. Conversion of numbers and strings
Depends entirely on the operator, such as + automatically converts left and right into numbers. Automatically convert left and right to string scalar variables
Start with $, such as $a=1;
In addition to assigning or declaring (in fact Perl variables do not need to be declared), other places need to have a $
Uninitialized variable value is udef, that is, the meaning of nothing, if the number operation is used similar to 0, the string is similar to the empty character print output results
print ' a ';
Output multiple separated by commas, print ' A ', ' B ', 2+3;
string interpolation to use double quotation marks, $name =zjk;print "I love $name";
Convert character code to character Chr (0XAAAA), Ord (' A ') converts string to code comparison operator
Compare Digital string
Equal = eq
Range != Ne
Less than < Lt
Greater than > Gt
Less than or equal <= Le
Greater than or equal = Ge
Get user Input
<STDIN> gets a line of input, ending with a carriage return, where carriage return is saved in the string
chomp () removes line breaks in the string
undef Value
Uninitialized variables
The uninitialized variable value is udef, which means nothing, and if the number operation is similar to 0, the string is similar to the null character, but undef is never empty.
The undefined () function determines if the variable is not undef, and if it is a false return, it is not returned
Reading to end of file returns undef scalar context
is to get scalar specific values such as: @array =qw (a b c), @str = @array;(list) $c =1+ @array;(array elements) are also @array two expressions are different.
Use a list expression in a scalar context: the resulting scalar, such as: $a =reverse (qw/i love/); get Evoli
Scalar: Enforces scalar contexts such as: @dogs =qw/black,red/;p rint ' There ', ' is ', @dogs, ' dogs '; here's what you want, but no, you need a scalar @dogs force switch Two, List and array @
List refers to a numeric value, an array is a list of numeric variables
The list is similar to the right half of the constant array, but with parentheses, such as: (' A ', ' B '), you can use any parentheses, and you can use # ' a ', ' B ' #
A member can be an expression that is updated every time it is used
also supports continuous (1..3) = (three-in-three), (1.1..3.3) = (three-to-three) automatic removal of decimal parts
The QW () indicates that the members inside are single-quote strings such as: QW (I love zjk) = (' I ' Love ' zjk ') and the members can be separated by any symbol, the space carriage return is OK
List assignment: You can also assign values between lists ($a, $b, $c) =qw (a b c); Array 1. Attention Points
Arrays and variables are exactly the same as the use of a square bracket $a[0]=a, and unlimited size, no need to define the length in advance, support non-continuous assignment, the middle of the spare is undef
Get the array length with $ #a, this length is the last element subscript, also say the actual length of the array to +1
Available negative numbers, 1 represents the last element, but exceeding the array size will report a critical error
Reference true arrays with the @ array name, such as @array=qw/a b c/;
Pop: Take out an array (it is an array instead of a list) the last element, $a =pop (@array), the parentheses can be omitted, if no assignment items such as pop @array means to delete the last element in the array
Push: Inserting into an array such as: Push (@array, 1), inserting 1 into an array, inserting multiple elements at once, and the operation must be an array
The unshift and shift actions correspond to the first element of the array
Splice operates on any other element in the array, such as $remove=splime @array, x,n; Delete Array[x] and its next n content, and assigns this part to $remove
Splime (@array, X,0,QW (a b c)); a position that does not delete and insert QW in array[x]
Empty array: @array = (); 2. Operators
Reverse: @array =reverse (@array) or reverse qw (); Flips the elements in the list or array, the tangent must be assigned to the array to change the value
Sort: @so =sort (@so) or sort qw (), sort, ascending, want to change the order again with reverse, sort the numbers by default to a string, so don't shoot.
Each: Returns the key value pair for the next element in the array each time it is called, my ($a, $b) = sort (@array); foreach statement
If no control variable, automatically use $_ as a control variable such as: foreach (1..10) {print $_} list Context
Get the value of a list based on the context of the operator
A scalar expression is used in a list context to get a list such as: @array =1*2; a member 3 @array =undef; a member is not initialized
<stdin>: @input =<stdin>;linux need to manually enter Ctrl+d, terminator, or wait for input
third, associative array%
Perl's unique, Key-value array, similar to hash, where elements are randomly inside.
1. Create
%array= (key1,value1,key2,value2);
%fruit= ("Apples" =>17, "Bananas" =>9, "oranges" = "none"), separating key and value with = =
%fruit= @fruit; Created with an array, the array element must be even 2. Add Modified data
$array {key}=value; Yes, $,key must also be in {}, if there is a key, it will change the value of key 3.
$a = $array {key1};
@lis =keys (%array); Take out all the keys in the array to the LIS array
@lis =values (%array); Removes all value from array to the LIS array
@lis =%array; All content is assigned to the LIS array
%array= @lis; it was created . 4. Delete
Delete $array {key}; Remove a key and its data
%array= (); empty four, subroutine
Can be placed above or below the file without declaring that the subroutine is global
1. Definition
Sub function name {...} 2. Call
The name of the & function; If the function name is the same as the name of the internal functions, it must be &, if it is defined after the call and the parameter does not have parentheses
No &:1 definition is not added & in front of the call, and the parentheses of the formal parameters are not added, and the 2-parameter calls with parentheses, such as func ($a), can be clearly seen as a function without adding 3. Return value
Each function has a return value, and the result of the last execution of the expression is the return value of the subroutine, so there is no need to make a function return value type
Note that this is very dangerous and a lot of potential return values, such as the last execution of print, although the surface does not seem to calculate the operation, but the print itself also has a return value, success or failure, so here must pay attention to
You can also use return to return a value, or execute a return of 4 when the program executes a large half . Formal parameters
The use of formal parameters is more flexible, can be used in the definition of the function of the formal parameters of things, as long as the inside with @_[0] call formal parameters @_[1] the second parameter, @_ all parameters equivalent to the array
There is no mandatory limit on the number of arguments at the time of the function definition, so that you can iterate through the parameters with a foreach function 5. Private variable my
My $a; scalar private, or my ($a, $b), list private, can only be used in a block of statements (that is, a curly brace), a My can only declare a scalar
My (\ $a, \ $b) =@_; Assigning a value to a private list State
State is similar to static, which is assigned when the first call is made, and the value remains unchanged after the function exits after the second call.
Perl statements are more flexible and incorporate languages such as Shell and C
1. For example
$opt {' h '} and Print_usage (), the preceding expression is non-empty to execute the next expression
$opt {' h '} or Print_usage (); the preceding expression is empty executes the next expression