Perl operator Usage Guide for Perl syntax-application Tips

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise logical operators scalar square root perl operator

Introduction to Perl Syntax Perl operators

1.Perl operator

1.1 Arithmetic operators

Perl is very different in variables and data paradigms and C. However, its operator and C are almost identical, except for the example conversion operator type of C, the pointer reference operator *PTR and the layout member selector, and the other C operators are Almost fully present in Perl, and Perl has new operators. Like in the character disposition aspect.

Some of the arithmetic operators of Perl to today support are:

+ addition operator
-Subtraction operator
* Multiplication operator
/except operators (floating-point operations only)
% modulo operator (integer operation only)

Here are some examples of Perl arithmetic:

$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 decrement operators:


Perl added the exponentiation operator: * *, see the following example:

$x = 2**3; #2 of the 3rd power
$y = 2**0.5; square root of #2
$z =-2**-3 #Results are:-0.125

1.2 Perl Syntax bitwise operator

The bitwise operator holds the disposition of the integer situation of the binary expression, and the result of the operation is an integer. If the manipulation number of a bitwise operator is a string or a fraction, Perl first converts them to integers and hints at them With a 32-bit long integer situation. Perl supports all C-speak operators:

| bit or operator
& Bit AND operator
Bitwise non-operator
<< bit left-shift operator >> bitwise RIGHT SHIFT operator

For the specific content of the bitwise operator, see C Talk about the content, here we have a few words, just a few examples:

$x = 5;
$y = 3;
Print$x| $y; #The result is 7 (binary hint: 111)
Print$x& $y; #The result is 1 (binary hint: 001)
Print$x<<2; #The result is 20 (binary hint: 10100) ]
Print$x>>1;# The result is 2 (binary hint: 10)

The force operator of the 1.3PERL syntax

The effect of the force operator is the value of the two manipulation number of the force. Perl converts the number of character manipulations to numbers before the force calculation is held. Perl uses a special string-specific operator to hold table operations on pure characters.

Logical operators of 1.4Perl syntax

The logical operator tests the value of a Boolean expression whose result is true or false. Perl thinks that every manipulation of a logical operator is a Boolean (that is, true or false). The logical operation of Perl Fubaoro:

|| logical OR operator;
&& logic and operators.

Perl presses the logical expression from left to right. Perl wills the logical expression when one of the operands of a logical OR operator is true, presumably when a manipulation of the logic and operator is false. Perl manipulates this short loop meter to quickly estimate The value of an expression. Yes, these two operators are also called short loops and short loops or.

In addition to the above two operators, the following three logical operators are also available:

Negative operators
?: Prerequisite Operators
, the sequence-valued character

Operator! Denies the Boolean value of the manipulation number, which is proportional to the logical non.? : The prerequisite operator, which has 3 operands, expresses the situation as follows: Condition?true-result:false-result

The following statement passes through the procedure? : operator, which enables the distinction between the right to worship:

$access = ($usereq ' cloud '? Root ': ' guest ');
The sequence operator (,) is not a logical operator in the harsh sense, because it does not check the manipulation number of true or false. Perl from left to right of the calculation of the comma operator, and return the right most operand, all good Operators are from C continuous, detailed use can refer to the use of C, here I am not tired to state.

The character operator of the 1.5Perl syntax

Since Perl itself was opened for the text disposal. Yes, it adds a lot of new string operators. Perl's string Operation Fubaoro:

The character concatenation operator
X string copy operator
=~ binds a variable to the modal State chamber
!~ binds a variable to the modal State chamber and takes a non

The first two operators are abbreviated. Now look at the example: print ' C '. ' A '. ' L ' x2 # output will be: Call;
The latter two operators are primarily used in the model room, and there will be a detailed explanation of the model chamber, and here's a few of them, so let's take a look at their examples to understand their impact:

$text = ' It rains today ';
Print ($text =~/rain/)? ' It's raining today ': ' It's not raining today ';
The example we see can be found whether the variable contains the strings we need.

Assignment operator for the 1.6PERL syntax

The assignment operator and the C-spoke assignment operator are very few, and the following are the various assignment operators:


The Lvalue of 1.7Perl grammar

In Perl and C speaking, Lvalue implies the entity to the left of the assignment operator. It also means that lvalue, like a variable, can assign a value to it. For example, you cannot assign a value to a string in the Perl command File, such as "Bob" =32 this statement is wrong! Because "Bob" is not a lvalue, but if the $bob assignment, such as $BOB=32, this statement is accurate! Because the variable $bob is a lvalue .
In Perl, any lvalue of meaning can only imply an entity. For example, the first statement below lists the value of the table (that is, the value of the array, but only the argument), @color is a lvalue, in the Second statement, the value of the table is assigned to 3 scalar variables, and 3 variables are lvalue:

@color = ($r, $g, $b);
($r, $g, $b) = @color;

When a Perl assignment operator disposes of a table, it is possible to dispose of the entire instrument incorrectly and assign values to only one or more of the table's elements:

@times [2,5,9]= (20,40,10);

The following statement assigns the first 2 values of the table to two scalars, and the other departments assign them to another table:

($arg 1, $arg 2, @reset) = @ARGV;

1.8Perl Syntax table operator

Perl includes some of the following table operators, and C does not:

, table organ Characters
.. Scale operator
X Table Copy operator

In front of "scalar and array variables", we have already planned the operator (I think it's easier to understand) the scale operators we used to build the scale of the array. But, please pay attention to its effect is far more than this , it can also be used as a return from the left-hand manipulation number to the right-hand operand between the order of the size of the integer (including the manipulation of both sides. The command file usually manipulates the scale operator to create a continuous integer table: @digits =0..9;

So we create a table with a value of (0,1,2,3,4,5,6,7,8,9)
Table copy operator is a very interesting thing, let's look at an example: @copy_3 = (1,2,3) x3; This table has 9 elements, its value is (1,2,3,1,2,3,1 ,2,3) how? It's very convenient!

1.9Perl syntax file operator

Perl greatly expands the file-handling operators. Perl has at least 27 operators that may test the file's information without opening the file, but unfortunately, because Perl was originally a Unix thing, it was a vast majority of operators on our popular platform: The WIN9X/NT system was not available. But the lucky one is not all the other 4 operators are absolutely available, and the effect is good! Here are the 4 operators:

-D test file is not a catalog;
-e test file exists;
-s test file size;
-W test file is writable;

The first two operators return a Boolean value (that is, true or false), and the 3rd operator returns the size of the file (in bytes as a return style).
How to use:

Copy Code code as follows:

If (-e ' Perl.exe ')
Print ' Filesizeis: '-S ' Perl.exe ';
Print ' can\ ' tfindperl.exe\n ';
(w ' somefile ') | | Die "cannotwritetosomefile\n";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.