SoFunction
Updated on 2025-04-07

Perl Learning Basic Memorandum

Array (1):

1) Initialize the array @array = ( "string" , "string" , "string" );
2) Use negative index loop to search print $array[-1];  #The output index is (-1 + 3) % 3 = 2 stringC
3) Dynamic growth $array[4] = "stringD"; #Although $array[3] has not been used yet, in the use of dynamic assignment $array[4], $array[3] has been assigned vacant
4) Negative index after dynamic growth print $array[-1]; # stringD with output index (-1 + 5) % 5 = 4
5) print @array; #Export all values ​​in the array without space concatenation
6) print "@array"; #Output space-separated list of all values ​​in the array

Array (2):

1) Different types of data can be saved in an array (strings, values, arrays)
2) $size = @array; This statement gives the array length value of the array @array to the scalar $size, but if @array is used directly in some functions, the array length may not be obtained.
3) $#arrayname, this special variable holds the end index value of the array named arrayname.
For example:@arr = ( 1,2,3,4,5 );
Then the value of $#arr is 4
$#arr=2;  This way, the array arr is truncated, its elements 4 and 5 are released, and the array length value is also changed.
4) @array[-1,4,7] will output elements with index values ​​of -1,4,7 respectively. Such a call returns not a scalar, but an array
5) Multi-dimensional arrays are defined using multi-layer square brackets
@D3array = (
[ [1,2,3,4] , [5,6,7] , [8,9,0] ],
[ [ 'str1' , 'str2' , 'str3' ] , [345 , 67 , 8930] ],
[ [ 4,6,7] , [2] , [ "sud" ] ]
);

The length of the array in each dimension in a multi-dimensional array does not need to be consistent, nor does the internal data elements need to be consistent.
The way to get the array length of the first dimension is $size = @array;
Second dimension $size = @{$array[$i]};
Third dimension $size = @{$array[$i][$j]};
....... ......
The key is that when the @ symbol is followed by an array variable, it can get its length, but when it exceeds one dimension, the expression to get the array variable must be wrapped in {}.

3. Execute Perl statements on the command line

1) perl -e 'Command Statement'  This allows you to execute Perl statements directly on the command line, but pay attention to the quotation mark matching rules.
2) perl -ne 'Command Statement'  filename This way, you can read the data in the filename line by line, and then process each line as a command statement.
For example a) perl -ne 'print;'
Output file data line by line in the command line interface
b) perl -ne 'print if /^192/ ' /etc/hosts > ~/
Write the contents of the host interpretation file in the UNIX like system to the ~/ file one by one, but only lines that meet 192 will be written, because the statement will perform filtering.

3) 'OS command' | perl -ne 'command statement' This way, the output of the OS command executed previously can be redirected to the perl command executed later.
For example a) ls -al | perl -ne 'print;'
List all subfiles and subdirectories in the current directory, and enter them as input data into the perl command, and then print them one by one.

Quotation Rules

1) The content in double quotes allows escape and variable parsing, the abbreviation is qq/content/
2) No characters in single quotes will be escaped and variables will not be parsed. The abbreviation is q/content/
3) If the content in the backtick marks appears, the command will be executed, and the result array will replace the command position, or as the data for assignment, the abbreviation is qx/content/
4) In the abbreviation above, the symbol pair/content/ can be replaced by other symbols, and the effects are the same, such as qq (content), qx! content! ,q+content+etc, but the letters seem to be impossible

Documentation rules

1) The starting tag does not use any quotation marks. The effect on document content is equivalent to using double quotes.
2) The starting tag uses single quotes. The effect on document content is equivalent to using single quotes.
3) The starting tag uses backticks. The effect on document content is equivalent to using backticks.

6. Variable initialization

Variables in Perl are allocated memory when they first appear. If it is not explicitly initialized, it will be assigned a value of 0 or an empty string. The specific expression depends on the context in which the variable appears.
Using the defined function, defined $var can check whether the variable has been initialized.
Using the undef function, undef $var can release the contents of a variable.

7. Special variables

1) $_, the value of this variable is often used as the default parameter value. For example, if print is called in this way; if no parameters are given, the value of $_ will be printed; when reading data using a file handle, if you do not specify which variable to use to save the read data, it will also be read into $_.


1) Hash table definition syntax:
 %aHash = (
'key1'  => "value1",
"key2" => 'value2',
    "key3" => 123,
    456 => "890"
) ;

2) The key values ​​in the hash table can be numbers, strings, arrays or even another hash table. However, if you want to use non-string key values, it is best to insert them into the hash table as a single key/value pair assignment rather than insert them at initialization.

3) For the hash table in 1) you can use the following operations to use hash slice:

Copy the codeThe code is as follows:

a) @aValues = qw ( 123 456 789 0 );
@aHash{'newKey1' , 'newKey2' , 'newKey3' , 'newKey4'} = @aValues;

In this way, a new hash table can be created. If the name is the same (except that the starting % becomes @), it will be inserted into the original hash table instead of creating.

b) For the modified aHash in 3), you can obtain a subset of its value set in the following way:

Copy the codeThe code is as follows:

@subSet = @aHash{ 'newKey1' , 'key1' , 456 };

The order in which elements are saved in the @subSet array is the order in which the keys are specified when assigning.
Among them, @hash table name is used in this way, called hash slice.

9. Array HASH nesting

Copy the codeThe code is as follows:

%aHash = (
 "key1" => "value1",
 "key2" => [
         "str1",[ 1,2,3,4,5 ],
 {
              "key2.1" => "value2.1";
              "key2.2" => "value2.2";
 }
         ],
       "key3" => {
             "key3.1" => "value3.1",
             "key3.2" => "value3.2",
        },
);
print "$aHash{'key2'}->[1][3]\n" ; output 4
print "$aHash{'key2'}->[2]->{'key2.1'}\n"; output value2.1
print "$aHash{'key3'}->{'key3.2'}\n"; output value3.2

In the above call, the -> symbol can be omitted.

10. Operator context
1) When the context of the operator is a numeric value, the beginning space in the operand will be skipped, and the first number in the operand will be directly found, and the subsequent string will be skipped. If the start string of the operand is not a space or a number, the operand is parsed to 0. An exception is that when the operand is in the form of a scientific number format, it is interpreted as a whole.

Copy the codeThe code is as follows:

$str1 = "  5  594asd";
$str2 = "10";
$str3 = "asd 10";
$str4 = "  4e3 asiddfi";
$sum = $str1 + $str2 + $str3 + $str4;  # The value of $sum is 4015, 5 + 10 + 0 + 4000

2) When the context of the operator is a string, all operands are parsed into strings.

11. Logical operator parsing
Each logical operator of Perl can be understood as short-circuited, that is, once a valid result is obtained, the subsequent expression will not continue to be parsed; and the parsed value of the last parsed logical expression is returned, except for the XOR operator.