1. Array interpolation in string
Like scalars, the contents of an array can also be interpolated into a string caused by double quotes
During interpolation, spaces for separation are automatically added between the elements of the array
@rocks = qw{ flitstone slate rubble }; print "quartz @rocks limestone\n";
After the array is interpolated, no additional spaces are added at the beginning and end
print "There rocks are: @rocks.\n"; print "There's nothing in the parens (@empty) here.\n";
How to put e-mail in double quotation marks
$email = "fred@bedrock.edu"; # wrong! This interpolates the @ bedrock array $email = "fred\@bedrock.edu"; # Escape @ operation $email = 'fred@bedrock.edu'; # Define a string directly in single quotes
When an element in an array is interpolated, it is replaced with the value of that element
@fred = qw(hello dolly); $y = 2; $x = "This is $fred[1]'s place"; # This is dolly's place $x = "This is $fred[$y-1]'s place"; # This is dolly's place
Note that the index expression will be treated as a normal string expression
If you want to write an open square bracket after a scalar variable, you need to separate the square bracket first so that it will not be recognized as part of the array reference
@fred = qw(eating rocks is wrong); $fred = "right"; # We want to say this is right[3] print "this is $fred[3]\n"; # Using $fred[3], print "wrong" print "this is ${fred}[3]\n"; # Print "right" (avoid ambiguity with curly braces) print "this is $fred"."[3]\n"; # Or print right (with separate strings) print "this is $fred\[3]\n"; # Or print right (use backslashes to avoid ambiguity)
2. foreach control structure
To process the data in the whole array or list, the foreach loop can traverse the values in the list item by item, extract and use them in turn
foreach $rock (qw/ bedrock slate lava/){ print "One rock is $rock.\n"; # Print the names of all 2 kinds of stones in turn }
Each time the loop iterates, the control variable gets a new value from the list
The control variable is not the copy of the list element, but the list element itself. If the value of the control variable is modified in the loop, the list element is modified at the same time
@rock = qw/ bedrock slate lava /; foreach $rock(@rocks){ $rock = "\t$rock"; # Add a tab before each element of @ rocks $rock .= "\n"; # At the same time, add a newline at the end } print "The rocks are:\n", @rocks; # Occupy one line each and use indent
When the loop ends, the loop variable is still the value before the loop is executed. If the loop variable is not assigned in the loop, it is undef
$rock = 'shale'; @rock = qw/ bedrock slate lava /; foreach $rock (@rocks){ ... } print "rock is still $rock\n"; # Print 'rock is still share'
The... In the code block is a placeholder, also known as the yada yada operator
3. Perl's favorite default variables$_
If you omit the control variable at the beginning of the foreach loop, perl uses the default variable$_
foreach (1..10){ print "I can count to $_!\n"; } $_ = "Yabba dabba doo\n"; print; # Default print$_
4. reverse operator
The reverse operator reads the values of the list (usually from an array) and returns the new list in reverse order
@fred = 6..10; @barney = reverse(@fred); # Return to 10, 9, 8, 7, 6 @wilma = reverse 6..10; # Same as above, but no additional array is required @fred = reverse @fred; # Save to the original array in reverse order
perl always evaluates the expression to the right of the equal sign before assignment
Reverse just returns the list in reverse order and does not modify the parameters given to it
reverse @fred; # Incorrect usage @fred = reverse @fred; # Correct usage
5. sort operator
Read the values of the list and sort them in the internal coding order of the characters
For a string, it is the code point that the character represents inside the computer
Uppercase characters precede lowercase characters, numbers precede letters, and punctuation is scattered
@rocks= qw/ bedrock slate rubble granite /; @sorted = sort(@rocks); # Return bedrock, granite, rule, slate @back = reverse sort @rocks; # In reverse order, slate, rule, granite and bedrock are returned @rocks = sort @rocks; @numbers = sort 97..102; # Get 100, 101, 102, 97, 98, 99
The sort operator sorts numbers as strings
According to the default sorting rule, any string beginning with 1 will be ranked before the string beginning with 9. In addition, like the reverse operator, the sorting operator will not modify the original parameters, but return a new list
sort @rocks; # error @rocks = sort @rocks; #correct
6. each operator
Use the each operator on arrays and hashes
Each time you call each on the array, you will return 2 values corresponding to the next element of the array
- Array index
- Array element
require v5.12; @rocks = qw/ bedrock slate rubble granite /; while( ($index, $value ) = each @rocks ){ print "$index:$value\n"; }
If you don't use each, you have to traverse from small to large according to the index, and then get the element value with the help of the index
@rocks = qw/ bedrock slate rubble granite /; foreach $index (0..$#rocks){ print "$index: $rocks[$index]\n"; }
7. Scalar context and list context
The same expression has different meanings when it appears in different places
When the operation value is operated according to the number, it is the digital result, while when the operation value is operated according to the string, it is the string result returned. The decisive factor is the operator, not various variables or direct quantities operated
@people = qw( fred barney betty ); @sorted = sort @people; # List context: barney, betty, fred $number = 42 + @people; # Scalar context: 42 + 3 = 45 @list = @people # Get 3 $n = @people;
Any expression can produce a list or scalar, depending on the context
8. Use the expression that produces the list in the scalar context
@backwards = reverse qw/ yabba dadda doo /; # You'll get doo, dabba, doo $backwards = reverse qw/ yabba dadda doo /; # Will get oodabdabdabbay
$fred = something; # scalar context @pebbles = something; # List context ($wilma, $betty) = something; # List context ($dino) = something; # Or list context
If you assign a value to a list (regardless of the number of elements in it), it is the list context
If you assign a value to an array, it is still the list context
Example of scalar context
$fred = something; $fred[3] = something; 123 + something something + 654 if (something) {...} while (something) {...} $fred[something] = something;
List context example
@fred = something; ($fred, $barney) = something; ($fred) = something; push @fred, something; foreach $fred (something) {...} sort something reverse something print something
9. Use expressions that produce scalars in the list context
@fred = 6 * 7; # Get a list with only a single element (42) @barney = "hello" . ' ' . "world"; @wilma = undef; # The result is a list of individual elements, and the element value is undefined @betty = (); # The correct way to empty an array
10. Force scalar context
Sometimes it is necessary to introduce scalar context in the list context. You can use the pseudo function scalar, which is not a real function. It is only used to tell Perl to switch to scalar context here
@rocks = qw ( talc quartz jade obsidian ); print "How many rocks do you have?\n"; print "I have ", @rocks, " rocks!\n"; # Wrong! This will output the names of various stones print "I have ", scalar @rocks, " rocks!\n"; # Correct. The number of stones is printed out
11. < stdin >
In a scalar context, < stdin > returns the next line of input data
In the list context, < stdin > returns the contents of all remaining lines, one element per line, until the end of the file
@line = <STDIN>; # Read standard input in list context
When the input data comes from a file, it reads the rest of the file.
However, if the source of input data is the keyboard, type Ctrl + D (Ctrl + Z) to inform the system that there will be no more input
If the person running the program is typing in three lines of data and informs the system of the completion of input (reaching the end of the file) through the corresponding keys, the final array will contain three elements, and each element is a string ending with a newline character, because these newline characters are also the input content
@line = <STDIN>; # Read the contents of all rows at once chomp(@lines); # Then remove the newline chomp(@lines = <STDIN>); # Read all lines at once, except line breaks