Introduction to Perl Operators
1. Perl Operators
1.1 Arithmetic Operators
Perl is similar to C in both variables and data examples, but its operators and C are almost the same, except for C's example conversion operator type, pointer Reference operator * ptr and layout member selector. Other C operators are almost completely present in Perl. Perl adds several new operators, such as character disposal.
◆ Perl currently supports several arithmetic operators:
+ Addition Operators
-Subtraction Operator
* Multiplication Operator
/Division operator (only for floating-point operations)
% Modulo operator (only Integer Operation)
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 the increment and decrease operators:
++ Increment
-- Decrease
◆ Perl adds the multiplication operator **. See the following example:
$ X = 2 ** 3; # Power 3 of 2
$ Y = 2*0.5; # the square root of 2
$ Z =-2 **-3; # The result is:-0.125.
1.2 bitwise operators in Perl syntax
Bitwise operators deal with the integer form of the Binary Expression, and the result is an integer. if the number of bitwise operators is a string or a fraction, Perl first converts them into integers and uses 32-bit long integers to imply them. perl supports all C-speaking operators:
| Bitwise OR operator
& Bitwise AND operator
. Bit non-Operator
<Bitwise shift operator> bitwise Shift Operator
For more information about bitwise operators, see the C-language-related content. Here we will not talk about it a few times. Here are just a few examples:
$ X = 5;
$ Y = 3;
Print $ x | $ y; # result is 7 (Binary suggestion: 111)
Print $ x & $ y; # result 1 (Binary suggestion: 001)
Print $ x <2; # result is 20 (Binary suggestion: 10100) print $ x> 1; # result is 2 (Binary suggestion: 10)
1.3Perl syntax-comparison Operators
The effect of the force-over operator is the value of the two manipulation numbers. before running a comparison operation, Perl converts the number of character manipulation to a number. perl uses special string comparison operators to perform table operations on pure characters.
Logical operators in 1.4Perl syntax
Logical operators test the value of a Boolean expression. The result is true or false. Perl indicates that each operator of a logical operator is a Boolean value (true or false). The logical operators of Perl include:
| Logic or operator;
& Logic and operators.
Perl calculates the logic expression from left to right. when the number of operations of a logic or operator is true, Perl will stop the calculation of logical expressions when the number of operations of a logic or operator is false. perl uses this short loop to calculate the value of the expression quickly. so, these two operators are also called short loop AND short loop OR.
In addition to the preceding two operators, there are also three logical operators:
! Negative Operator
? : Prerequisite Operator
, Count-by-count Operator
Operator! Denying the Boolean value of the number of operations is commensurate with the logical non .? : Prerequisite operator, which has three manipulation numbers. The expression is condition? True-result: false-result
The following statements are passed through the process? : Operator to differentiate the waiting permissions:
$ Access = ($ usereq 'cloud '? 'Root': 'guest ');
The sequential operator (,) is not a logical operator in a harsh sense, because it does not check whether the number of operations is true or false. perl calculates the comma operator from left to right and returns the rightmost number of operations. The operators are continuous from C. For details, refer to the usage in C, I will not go into detail here.
Character operators in 1.5Perl syntax
Because Perl itself is developed for text processing, it adds many new string operators. Perl's string operators include:
. Character concatenation operator
X string copy Operator
= ~ Bind a variable to the mode room
!~ Bind a variable to the mode room.
The comparison between the first two operators is simple. Now let's look at the example: print 'C'. 'A'. 'l' x2; # The output result will be: Call;
The last two operators are mainly used in the mode room. In the future, we will provide a detailed explanation of the mode room. We will not talk about them here. Let's take a look at their examples to understand their role:
$ Text = 'rain this day ';
Print ($ text = ~ /Rain /)? 'Rain this day': 'No rain this day ';
The example we see shows whether the variable contains the necessary strings.
Assignment operator in 1.6Perl syntax
The value assignment operator differs from the value assignment operator in C. The following are various value assignment operators:
= + =-= * =/= % = | = & =
^ = ~ = <=> = ** =. = X =
Lvalue of 1.7Perl syntax
In Perl and C, lvalue implies the entity on the left of the value assignment operator. in other words, lvalue is the same as a variable and can be assigned to it. for example, you cannot assign a value to a string in a Perl Command file. For example, the "Bob" = 32 statement is incorrect! Because "Bob" is not an lvalue, if $ Bob is assigned a value, for example, $ Bob = 32, this statement is accurate! The variable $ Bob is an lvalue.
In Perl, The lvalue of any meaning can only imply one entity. for example, the first statement below lists the table values (that is, the values of the array, but they are different). @ color is an lvalue. In the second statement, assign the table value to three scalar variables. The three variables are lvalue:
@ Color = ($ r, $ g, $ B );
($ R, $ g, $ B) = @ color;
When the assignment operator of Perl processes a table, the entire table can be disposed of without errors, but only assigned values for a certain element of the table:
@ Times [2, 5, 9] = (20, 40, 10 );
The following statement assigns the first two values of the table to two scalar values, and other departments to the other table:
($ Arg1, $ arg2, @ reset) = @ ARGV;
1.8Perl syntax-Table Operators
Perl includes the following table operators, but c does not:
, Table Operator
... Scale operator
X table replication Operator
When we first introduced "scalar and array variables", we planned a scale operator (I thought it was called a separator, which is easier to understand). When we used to create an array subscript scale, also used! However, please pay attention that its function is far more than this. It can also be used as a return integer between the number of operations on the left and the number of operations on the right (include the number of operations on both sides ). the number command file creates a continuous integer table by manipulating the scale operator at the same time: @ digits = 0 .. 9;
In this way, we created a table whose value is)
The table replication operator is a funny stuff. Let's look at an example: @ copy_3 = (, 3) x3; this table has nine elements whose values are (, 1, 2, 3, 2, 3) how is it? Very convenient!
1.9Perl syntax-file Operators
Perl greatly extends the file processing operators. perl has at least 27 operators that can test file information without opening a file. Unfortunately, Perl was originally UNIX, we use a vast majority of operators on our popular platform: The WIN9X/NT system cannot be used. however, fortunately, there are four other operators in the full number, which are absolutely usable and have a good effect! Here are the four operators:
-D. Whether the test file is a directory;
-E: whether the test file exists;
-S: the details of the test file;
-W: whether the test file is writable;
The first two operators return a Boolean value (true or false). The first two operators return the size of the file (in bytes ).
Method of exploits:
Copy codeThe Code is as follows: if(-e'Perl.exe ')
{
Print 'filesizeis: '-s'Perl.exe ';
}
Else
{
Print 'can \ 'tfindPerl.exe \ n ';
}
(-W 'somefile') | die "CannotwritetoSomeFile \ n ";