Perl syntax Introduction to Perl operators
Operators
1.1 Arithmetic operator
Although Perl is very different from C in variables and data examples, its operators are almost the same as C. Except for C's example conversion operator type, pointer reference operator *ptr and layout member selector, other C operators are almost completely presented in Perl. Perl has added several new operators, such as in character disposal.
◆There are several types of arithmetic operators that Perl has supported today:
+Addition operator
-Subtraction operator
*Multiple operator
/Divide operator (only for floating point numbers)
%module operator (only for integers)
Here are some examples of Perl arithmetic operations:
$x=2.5;
$y=3;
print($x+2*$y);
print(7/$y);
printint(7/$y);
print(7%$y);
print(7.5%$y);
Perl also supports increment and decreasing operators:
++Receive
--Decreasing
◆Perl has added a multiplier operator: **, please see the following example:
$x=2**3;#2 to the 3rd power
$y=2**0.5;#2 square root
$z=-2**-3;#The result is:-0.125
1.2 Perl syntax bit operator
The bit operator deals with the integer situation of a binary expression, and the operation result is an integer. If the manipulation numbers of bit operators are strings or fractions, Perl starts to convert them into integers and implies them with the 32-bit long integer situation. Perl supports all C-talk operators:
|Bit or operator
& bits and operators
.Bit non-operator
<<Bitle left shift operator>>Bit right shift operator
For the specific content of bit operators, please refer to the content related to C speech. We will talk a little bit here, just give a few examples:
$x=5;
$y=3;
print$x|$y;# result is 7 (binary hint: 111)
print$x&$y;# result is 1 (binary hint: 001)
print$x<<2;# result is 20 (binary hint: 10100) print$x>>1;# result is 2 (binary hint: 10)
1.3 Perl syntax ratio operator
The power operator is the value of two manipulators of force. Before Perl performs a comparative operation, it will convert the character manipulation number into a number. Perl uses a special string force operator to perform table operations on pure characters.
1.4 Logical operators of Perl syntax
Logical operators test the value of a Boolean expression, and the result is true or false. Perl thinks that each manipulation of a logical operator is a Boolean value (i.e. true or false). Perl's logical operators include:
||Logistic or operator;
&&Logistics and Operators.
Perl calculates logical expressions in order from left to right. When a manipulation number of logic or operators is true, probably when a manipulation number of logic and operators is false, Perl will stop calculating the logical expression. Perl manipulates this short-loop meter value to quickly calculate the value of an expression. Therefore, these two operators are also called short loop AND and short loop OR.
In addition to the above two operators, there are three logical operators:
!Negative operator
?:Prerequisite operator
, order value symbol
Operator! Denied the Boolean value of manipulated numbers, which is equivalent to logical non- ? : The premise operator has 3 manipulation numbers, and the expression is: condition?true-result:false-result
What is the following statement through the process? : operator, realizes the distinction between waiting permissions:
$access=($usereq's flow cloud'?'root':'guest');
The order operator (,) is not a logical operator in a strict sense, because it does not check the authenticity of manipulation numbers. Perl calculates comma operator from left to right and returns the rightmost manipulation number. All operators are continuous from C. For detailed use, you can refer to the usage in C. I won't explain it here.
1.5 Character operators of Perl syntax
Because Perl itself opened up for text disposal. Therefore, it adds a lot of new string operators. Perl's string operators include:
. Character conjunction operator
x string copy operator
=~Bind a variable to the pattern room
!~Bind a variable to the pattern room and take non
The first two operators are simple. Let’s take a look at the example at this moment: print'C'.'a'.'l'x2;#The output result will be: Call;
The last two operators are mainly used in pattern room building. In the future, there will be a detailed explanation of pattern room building. I will talk about it here. Let’s take a look at their examples to understand their effects:
$text='It's raining today';
print($text=~/Rain/)?'It's raining today':'It's not raining today';
The examples we see can invent whether variables contain strings that we need.
1.6 Assignment operators for Perl syntax
The assignment operators that are speaking with C are not much different. The following are various assignment operators:
=+=-=*=/=%=|=&=
^=~=<<=>>=**==.=x=
1.7 Perl syntax Lvalue
In Perl and C speaking, lvalue implies the entity to the left of the assignment operator. In other words, lvalue can be assigned a value like a variable. For example, in the Perl command file, it is impossible to assign a value to a string. For example, the statement "Bob"=32 is wrong! Since "Bob" is not an lvalue, if $Bob is assigned, such as $Bob=32, this statement is accurate! Since the variable $Bob is an lvalue.
In Perl, any meaningful lvalue can only imply one entity. For example, the first statement below lists the values of the table (that is, the values of the array are just different), @color is an lvalue. In the second statement, the value of the table is assigned to 3 scalar variables, and the 3 variables are lvalue:
@color=($r,$g,$b);
($r,$g,$b)=@color;
When Perl's assignment operator handles the table, it can handle the entire table without compromising the error, but only perform assignments for one or several elements of the table:
@times[2,5,9]=(20,40,10);
The following statement assigns the first two values of the table to two scalars, and other departments to another table:
($arg1,$arg2,@reset)=@ARGV;
1.8 Table operators of Perl syntax
Perl includes the following table operators, but c does not:
, table mechanism symbol
..Scale operator
x table copy operator
When we introduced "scalars and array variables", we have already planned the mechanism character (I thought it was called the separation character, which is easier to understand). The scale operator we have also used when we used it to establish the scale of array subscripts! However, please pay attention to its effectiveness far more than this. It can also be used as a return to the scale of integers in sequence from the left manipulation number to the right manipulation number (including manipulation numbers of both sides). The command file uses the scale operator simultaneously to create a continuous integer table: @digits=0..9;
So we create a table with values (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
The table copy operator is a very interesting thing. Let's take an example: @copy_3=(1,2,3)x3; This table has 9 elements and its value is (1,2,3,1,2,3,1,2,3) How about it? Very convenient!
1.9 File operators of Perl syntax
Perl greatly extends the file handling operator. Perl has at least 27 operators that can test the file information without opening the file, but unfortunately, because Perl was originally a UNIX thing, so most operators are on our popular platforms: the WIN9X/NT system cannot be used. Fortunately, there are 4 other operators that are absolutely available and have good results! Here are the 4 operators:
-d test file is not a directory;
-e Test whether the file exists;
-s test file size;
-w test whether the file is writable;
The first two operators return a Boolean value (i.e. true or false), and the third operator returns the size of the file (using bytes as the return format). Below is
How to use:
if(-e'')
{
print'Filesizeis:'-s'';
}
else
{
print'Can\'\n';
}
(-w'SomeFile')||die"CannotwritetoSomeFile\n";