Perl - 运算符

算术运算符

Sr.No.Operator & 描述
1

+ (加法)

\$a + \$b=30

2

- (减法)

\$a-\$b=-10

3

* (乘法)

\$a * \$b=200

4

/( 除)

\$b/\$a=2

5

％(取模)

\$b％\$a=0  (整除后的余数)

6

**(指数)

\$a** \$b  10的20次方

```#!/usr/local/bin/perl

\$a=21;
\$b=10;

print "Value of\\$a=\$a and value of\\$b=\$b\n";

\$c=\$a + \$b;
print 'Value of \$a + \$b=' . \$c . "\n";

\$c=\$a - \$b;
print 'Value of \$a - \$b=' . \$c . "\n";

\$c=\$a * \$b;
print 'Value of \$a * \$b=' . \$c . "\n";

\$c=\$a/\$b;
print 'Value of \$a/\$b=' . \$c . "\n";

\$c=\$a % \$b;
print 'Value of \$a % \$b=' . \$c. "\n";

\$a=2;
\$b=4;
\$c=\$a ** \$b;
print 'Value of \$a ** \$b=' . \$c . "\n";```

```Value of \$a=21 and value of \$b=10
Value of \$a + \$b=31
Value of \$a - \$b=11
Value of \$a * \$b=210
Value of \$a/\$b=2.1
Value of \$a % \$b=1
Value of \$a ** \$b=16```

关系运算符

Sr.No.Operator & 描述
1

==(等于)

(\$a == \$b) is not true。

2

!=(不等于)

(\$a!= \$b) is true。

3

<=>

(\$a <=> \$b) returns -1.

4

> (greater than)

(\$a > \$b) is not true.

5

< (less than)

(\$a < \$b) is true.

6

>= (greater than or equal to)

(\$a >= \$b) is not true.

7

<= (less than or equal to)

(\$a <= \$b) is true.

```#!/usr/local/bin/perl

\$a = 21;
\$b = 10;

print "Value of\\$a=\$a and value of\\$b=\$b\n";

if( \$a == \$b ) {
print "\$a ==\\$b is true\n";
} else {
print "\\$a ==\\$b is not true\n";
}

if( \$a != \$b ) {
print "\\$a !=\\$b is true\n";
} else {
print "\\$a !=\\$b is not true\n";
}

\$c = \$a <=> \$b;
print "\\$a <=>\\$b returns \$c\n";

if( \$a > \$b ) {
print "\\$a >\\$b is true\n";
} else {
print "\\$a >\\$b is not true\n";
}

if( \$a >= \$b ) {
print "\\$a >=\\$b is true\n";
} else {
print "\\$a >=\\$b is not true\n";
}

if( \$a < \$b ) {
print "\\$a <\\$b is true\n";
} else {
print "\\$a <\\$b is not true\n";
}

if( \$a <= \$b ) {
print "\\$a <=\\$b is true\n";
} else {
print "\\$a <=\\$b is not true\n";
}```

```Value of \$a=21 and value of \$b=10
\$a == \$b is not true
\$a != \$b is true
\$a <=> \$b returns 1
\$a > \$b is true
\$a >= \$b is true
\$a < \$b is not true
\$a <= \$b is not true```

Sr.No.Operator & 描述
1

lt

(\$a lt \$b) is true

2

gt

(\$a gt \$b) is false。

3

le

(\$a le \$b) is true

4

ge

-(\$a ge \$b) is false

5

eq

-(\$a eq \$b) is false

6

ne

(\$a ne \$b) is true

7

cmp

-(\$a cmp \$b) is -1。

```#!/usr/local/bin/perl

\$a="abc";
\$b="xyz";

print "Value of\\$a=\$a and value of\\$b=\$b\n";

if( \$a lt \$b ) {
print "\$a lt\\$b is true\n";
} else {
print "\\$a lt\\$b is not true\n";
}

if( \$a gt \$b ) {
print "\\$a gt\\$b is true\n";
} else {
print "\\$a gt\\$b is not true\n";
}

if( \$a le \$b ) {
print "\\$a le\\$b is true\n";
} else {
print "\\$a le\\$b is not true\n";
}

if( \$a ge \$b ) {
print "\\$a ge\\$b is true\n";
} else {
print "\\$a ge\\$b is not true\n";
}

if( \$a ne \$b ) {
print "\\$a ne\\$b is true\n";
} else {
print "\\$a ne\\$b is not true\n";
}

\$c=\$a cmp \$b;
print "\\$a cmp\\$b returns \$c\n";```

```Value of \$a=abc and value of \$b=xyz
abc lt \$b is true
\$a gt \$b is not true
\$a le \$b is true
\$a ge \$b is not true
\$a ne \$b is true
\$a cmp \$b returns -1```

赋值运算符

Sr.No.Operator & 描述
1

=

\$c= \$a + \$b 将\$a + \$b的值分配给\$c

2

+=

\$c +=\$a等效于\$c= \$c + \$a

3

-=

\$c-= \$a等效于\$c=\$c-\$a

4

*=

\$c *=\$a等效于\$c=\$c * \$a

5

/=

\$c/= \$a等同于\$c=\$c/\$a

6

%=

\$c％= \$a等同于\$c=\$c％a

7

** =

\$c **=\$a等效于\$c=\$c ** \$a

```#!/usr/local/bin/perl

\$a=10;
\$b=20;

print "Value of\\$a=\$a and value of\\$b=\$b\n";

\$c=\$a + \$b;
print "After assignment value of\\$c=\$c\n";

\$c += \$a;
print "Value of\\$c=\$c after statement\\$c +=\\$a\n";

\$c -= \$a;
print "Value of\\$c=\$c after statement\\$c -=\\$a\n";

\$c *= \$a;
print "Value of\\$c=\$c after statement\\$c *=\\$a\n";

\$c /= \$a;
print "Value of\\$c=\$c after statement\\$c /=\\$a\n";

\$c %= \$a;
print "Value of\\$c=\$c after statement\\$c %=\\$a\n";

\$c=2;
\$a=4;
print "Value of\\$a=\$a and value of\\$c=\$c\n";
\$c **= \$a;
print "Value of\\$c=\$c after statement\\$c **=\\$a\n";```

```Value of \$a=10 and value of \$b=20
After assignment value of \$c=30
Value of \$c=40 after statement \$c += \$a
Value of \$c=30 after statement \$c -= \$a
Value of \$c=300 after statement \$c *= \$a
Value of \$c=30 after statement \$c /= \$a
Value of \$c=0 after statement \$c %= \$a
Value of \$a=4 and value of \$c=2
Value of \$c=16 after statement \$c **= \$a```

按位运算符

```\$a=0011 1100

\$b=0000 1101

-----------------

\$a&\$b=0000 1100

\$a | \$b=0011 1101

\$a ^ \$b=0011 0001

〜\$a=1100 0011```

Perl语言支持以下Bitwise运算符，假设\$a=60；和\$b=13

Sr.No.Operator & 描述
1

&

2

|

3

^

4

5

<<

6

>>

```#!/usr/local/bin/perl

use integer;

\$a = 60;
\$b = 13;

print "Value of\\$a=\$a and value of\\$b=\$b\n";

\$c = \$a & \$b;
print "Value of\\$a &\\$b=\$c\n";

\$c = \$a | \$b;
print "Value of\\$a |\\$b=\$c\n";

\$c = \$a ^ \$b;
print "Value of\\$a ^\\$b=\$c\n";

\$c = ~\$a;
print "Value of ~\\$a=\$c\n";

\$c = \$a << 2;
print "Value of\\$a << 2=\$c\n";

\$c = \$a >> 2;
print "Value of\\$a >> 2=\$c\n";```

```Value of \$a=60 and value of \$b=13
Value of \$a & \$b=12
Value of \$a | \$b=61
Value of \$a ^ \$b=49
Value of ~\$a=-61
Value of \$a << 2=240
Value of \$a >> 2=15```

逻辑运算符

Perl语言支持以下逻辑运算符。假设变量\$a=true，变量\$b= false，然后-

Sr.No.Operator & 描述
1

and

2

&&

3

or

4

||

5

not

```#!/usr/local/bin/perl

\$a=true;
\$b=false;

print "Value of\\$a=\$a and value of\\$b=\$b\n";

\$c=(\$a and \$b);
print "Value of\\$a and\\$b=\$c\n";

\$c=(\$a  && \$b);
print "Value of\\$a &&\\$b=\$c\n";

\$c=(\$a or \$b);
print "Value of\\$a or\\$b=\$c\n";

\$c=(\$a || \$b);
print "Value of\\$a ||\\$b=\$c\n";

\$a=0;
\$c=not(\$a);
print "Value of not(\\$a)= \$c\n";```

```Value of \$a=true and value of \$b=false
Value of \$a and \$b=false
Value of \$a && \$b=false
Value of \$a or \$b=true
Value of \$a || \$b=true
Value of not(\$a)= 1```

引用运算符

Perl语言支持以下类似Quote的运算符。在下表中，{}代表您选择的任何一对定界符。

Sr.No.Operator & 描述
1

q {}

2

qq {}

3

qx {}

```#!/usr/local/bin/perl

\$a=10;

\$b=q{a=\$a};
print "Value of q{a =\\$a}=\$b\n";

\$b=qq{a=\$a};
print "Value of qq{a =\\$a}=\$b\n";

# unix command execution
\$t=qx{date};
print "Value of qx{date}=\$t\n";```

```Value of q{a=\$a}=a=\$a
Value of qq{a=\$a}=a=10
Value of qx{date}=Thu Feb 14 08:13:17 MST 2013```

其他运算符

Perl语言支持以下杂类运算符。假设变量a=10，变量b=20，则-

Sr.No.Operator & 描述
1

.

2

x

3

..

4

++

5

-

6

->

```#!/usr/local/bin/perl

\$a="abc";
\$b="def";

print "Value of\\$a =\$a and value of\\$b=\$b\n";

\$c=\$a . \$b;
print "Value of\\$a .\\$b=\$c\n";

\$c="-" x 3;
print "Value of\"-\" x 3=\$c\n";

@c=(2..5);
print "Value of (2..5)[email protected]\n";

\$a=10;
\$b=15;
print "Value of\\$a =\$a and value of\\$b=\$b\n";

\$a++;
\$c=\$a ;
print "Value of\\$a after\\$a++=\$c\n";

\$b--;
\$c=\$b ;
print "Value of\\$b after\\$b--=\$c\n";```

```Value of \$a=abc and value of \$b=def
Value of \$a . \$b=abcdef
Value of "-" x 3=---
Value of (2..5)=2 3 4 5
Value of \$a=10 and value of \$b=15
Value of \$a after \$a++=11
Value of \$b after \$b--=14```

运算符优先级

```left	terms and list operators (leftward)
left	->
nonassoc	++ --
right	**
right	! ~\and unary + and -
left	=~ !~
left	*/% x
left	+ - .
left	<< >>
nonassoc	named unary operators
nonassoc	< > <= >= lt gt le ge
nonassoc	== != <=> eq ne cmp ~~
left	&
left	| ^
left	&&
left	|| //
nonassoc	..  ...
right	?:
right	= += -= *= etc.
left	, =>
nonassoc	list operators (rightward)
right	not
left	and
left	or xor```

```#!/usr/local/bin/perl

\$a=20;
\$b=10;
\$c=15;
\$d=5;
\$e;

print "Value of\\$a =\$a,\\$b=\$b,\\$c=\$c and\\$d=\$d\n";

\$e=(\$a + \$b) * \$c/\$d;
print "Value of (\\$a +\\$b) *\\$c/\\$d is =\$e\n";

\$e=((\$a + \$b) * \$c )/\$d;
print "Value of ((\\$a +\\$b) *\\$c)/\\$d is =\$e\n";

\$e=(\$a + \$b) * (\$c/\$d);
print "Value of (\\$a +\\$b) * (\\$c/\\$d ) is =\$e\n";

\$e=\$a + (\$b * \$c )/\$d;
print "Value of\\$a + (\\$b *\\$c )/\\$d is =\$e\n";```

```Value of \$a=20, \$b=10, \$c=15 and \$d=5
Value of (\$a + \$b) * \$c/\$d is=90
Value of ((\$a + \$b) * \$c)/\$d is=90
Value of (\$a + \$b) * (\$c/\$d ) is=90
Value of \$a + (\$b * \$c )/\$d is=50```

相关文章

Vue开发实战 -〔唐金州 - 53讲〕

TypeScript开发实战 -〔梁宵 - 47讲〕

SolidJS 中的条件样式

React 什么时候调用组件构造函数，执行流程是怎样的？