📜  珀尔 |运算符 |套装 – 2

📅  最后修改于: 2022-05-13 01:54:25.448000             🧑  作者: Mango

珀尔 |运算符 |套装 – 2

运算符是任何编程语言的主要构建块。运算符允许程序员对操作数执行不同类型的操作。在 Perl 中,对于不同类型的操作数(如标量和字符串),运算符符号会有所不同。 Perl Operators Set – 1中已经讨论了一些运算符符。其余运算符将在本文中讨论:

  • 引用式运算符
  • 字符串操作运算符
  • 范围运算符
  • 自动递增和递减运算符
  • 箭头运算符

引用式运算符

在这些运算符中,{} 将代表用户选择的一对分隔符。在此类别中,有以下三个运算符:

  • q{ } :它将用单引号(”)括起来一个字符串,并且不能插入字符串变量。例如: q{geek} 给出 'geek'。
  • qq{ } :它将一个字符串括在双引号(“”)中,并可以插入字符串变量。例如: qq{geek} 给出“geek”。
  • qx{ } :它将用反引号(“)括起一个字符串。例如: qq{geek} 给出 `geek`。

例子:

Perl
# Perl program to demonstrate the Quote
# Like Operators
  
#!/usr/local/bin/perl
  
# taking a string variable
$g = "Geek";
  
# using single quote Operators
# this operator will not 
# interpolate the string variable
$r = q{$g};
  
print "Value of g is = $r\n";
  
# using double quote Operators
# this operator will interpolate
# the string variable
$r = qq{$g};
  
print "Value of g is = $r\n";
  
# Executing unix date command
$d = qx{date};
  
print "Value of qx{date} = $d";


Perl
# Perl program to demonstrate the
# string operators
  
#!/usr/bin/perl
  
# Input first string 
$first_string = "Geeks";
  
# Input second string 
$second_string = "forGeeks";
  
# Implement Concatenation operator(.) 
$concat_string = $first_string.$second_string;
  
# displaying concatenation string result
print "Result of Concatenation Operator = $concat_string\n";
  
# Input a string 
$string = "GeeksforGeeks "; 
   
# Using Repetition operator(x)
$str_result = $string x 4; 
   
# Display output
# print string 4 times 
print "Result of Repetition Operator = $str_result";


Perl
# Perl program to demonstrate the
# Range operators
  
#!/usr/bin/perl
  
# using range operator  
@res = (1..4);
  
# Display output
print "Result of Range Operator = @res";


Perl
# Perl program to demonstrate the Auto 
# Increment and Decrement Operator
  
#!/usr/local/bin/perl
  
# taking a  variable
$g = 10;
  
# using pre Increment
$res = ++$g;
  
print "Value of res is = $res\n";
print "Value of g is = $g\n";
  
# taking a  variable
$g1 = 15;
  
# using post Increment
$res1 = $g1++;
  
print "Value of res1 is = $res1\n";
print "Value of g1 is = $g1\n";
  
# taking a  variable
$g2 = 20;
  
# using pre Decrement
$res2 = --$g2;
  
print "Value of res2 is = $res2\n";
print "Value of g2 is = $g2\n";
  
# taking a  variable
$g3 = 25;
  
# using post Decrement
$res3 = $g3--;
  
print "Value of res3 is = $res3\n";
print "Value of g3 is = $g3\n";


Perl
# Perl program to demonstrate the Arrow Operator
  
#!/usr/local/bin/perl
  
use strict;
use warnings;
  
# reference to array
my $arr1 = [4,5,6];
  
# array inside array
my $arr2 = [4,5,[6,7]];
  
# reference to hash
my $has1 = {'a'=>1,'b'=>2};
  
# hash inside hash
my $has2 = {'a'=>1,'b'=>2,'c'=>[1,2],'d'=>{'x'=>3,'y'=>4}};
  
# using arrow operator
print "$arr1->[0]\n";
print "$arr2->[1]\n";
print "$arr2->[2][0]\n";
print "$has2->{'a'}\n";
print $has2->{'d'}->{'x'},"\n";
print $has2->{'c'}->[0],"\n";


输出:

Value of g is = $g
Value of g is = Geek
Value of qx{date} = Sun Aug 12 14:19:43 UTC 2018

字符串操作运算符

字符串是标量变量,在 Perl 中以($)符号开头。字符串由用户在单引号(”)或双引号(“”)中定义。 Perl 中有不同类型的字符串运算符,如下所示:

  • 连接运算符 (.) :Perl字符串与点 (.)符号连接。在 Perl 中使用点 (.) 符号代替 (+) 符号。
  • 重复运算符 (x): x运算符在其左侧接受一个字符串,在其右侧接受一个数字。它将返回左侧的字符串,其重复次数与右侧的值相同。

例子:

Perl

# Perl program to demonstrate the
# string operators
  
#!/usr/bin/perl
  
# Input first string 
$first_string = "Geeks";
  
# Input second string 
$second_string = "forGeeks";
  
# Implement Concatenation operator(.) 
$concat_string = $first_string.$second_string;
  
# displaying concatenation string result
print "Result of Concatenation Operator = $concat_string\n";
  
# Input a string 
$string = "GeeksforGeeks "; 
   
# Using Repetition operator(x)
$str_result = $string x 4; 
   
# Display output
# print string 4 times 
print "Result of Repetition Operator = $str_result";

输出:

Result of Concatenation Operator = GeeksforGeeks
Result of Repetition Operator = GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks 

注意:要了解更多关于 Perl 中的字符串运算符,您可以参考Perl 字符串运算符一文。

范围运算符(..)

在 Perl 中,范围运算符用于创建指定元素的指定序列范围。因此,此运算符用于创建指定的序列范围,其中开始和结束元素都将包含在内。例如, 7 .. 10 将创建一个类似 7、8、9、10 的序列。
例子:

Perl

# Perl program to demonstrate the
# Range operators
  
#!/usr/bin/perl
  
# using range operator  
@res = (1..4);
  
# Display output
print "Result of Range Operator = @res";

输出:

Result of Range Operator = 1 2 3 4

自动递增和自动递减运算符

Auto Increment(++):增加一个整数的值。当放置在变量名之前(也称为预增量运算符),它的值会立即增加。例如,++$x。并且当它放在变量名之后(也称为后自增运算符),它的值会暂时保留到执行该语句之前,并在执行下一条语句之前更新。例如,$x++。
Auto Decrement Operator(–):递减一个整数的值。当放置在变量名之前(也称为预减运算符),它的值会立即递减。例如,-$x。并且当它放在变量名之后(也称为后减运算符)时,它的值会暂时保留到执行该语句之前,并在执行下一条语句之前更新。例如,$x–。
注意:递增和递减运算符称为一元运算运算符,因为它们使用单个操作数。
例子:

Perl

# Perl program to demonstrate the Auto 
# Increment and Decrement Operator
  
#!/usr/local/bin/perl
  
# taking a  variable
$g = 10;
  
# using pre Increment
$res = ++$g;
  
print "Value of res is = $res\n";
print "Value of g is = $g\n";
  
# taking a  variable
$g1 = 15;
  
# using post Increment
$res1 = $g1++;
  
print "Value of res1 is = $res1\n";
print "Value of g1 is = $g1\n";
  
# taking a  variable
$g2 = 20;
  
# using pre Decrement
$res2 = --$g2;
  
print "Value of res2 is = $res2\n";
print "Value of g2 is = $g2\n";
  
# taking a  variable
$g3 = 25;
  
# using post Decrement
$res3 = $g3--;
  
print "Value of res3 is = $res3\n";
print "Value of g3 is = $g3\n";

输出:

Value of res is = 11
Value of g is = 11
Value of res1 is = 15
Value of g1 is = 16
Value of res2 is = 19
Value of g2 is = 19
Value of res3 is = 25
Value of g3 is = 24

箭头运算符(->)

此运算符用于从类或对象中取消引用变量或方法。示例:$ob->$x 是从对象 $ob 访问变量 $x 的示例。大多数情况下,此运算符用作对散列或数组的引用,以访问散列或数组的元素。
例子:

Perl

# Perl program to demonstrate the Arrow Operator
  
#!/usr/local/bin/perl
  
use strict;
use warnings;
  
# reference to array
my $arr1 = [4,5,6];
  
# array inside array
my $arr2 = [4,5,[6,7]];
  
# reference to hash
my $has1 = {'a'=>1,'b'=>2};
  
# hash inside hash
my $has2 = {'a'=>1,'b'=>2,'c'=>[1,2],'d'=>{'x'=>3,'y'=>4}};
  
# using arrow operator
print "$arr1->[0]\n";
print "$arr2->[1]\n";
print "$arr2->[2][0]\n";
print "$has2->{'a'}\n";
print $has2->{'d'}->{'x'},"\n";
print $has2->{'c'}->[0],"\n";

输出:

4
5
6
1
3
1

要记住的要点:

  1. 运算符关联性:用于决定方程或表达式是从左到右还是从右到左计算。评价的顺序很重要。有时它可能从两边看起来都一样,但它会产生很大的不同。
  2. Perl Arity: arity 可以定义为运算运算符操作的操作数的数量。
    • Nullary 运算符 :这些运算符对零操作数进行操作。
    • 一元运算符:这些运算符对一个操作数进行操作。
    • 二元运算符:这些运算符对两个操作数进行操作。
    • 列表运算符:这些运算符对操作数列表进行操作。
  3. Perl Fixity:它可以定义为运算符相对于其操作数的位置。
    • 缀运算符:这些运算符位于其操作数之间。例如,5 + 8,这里,+运算符位于操作数 5 和 8 之间
    • 前缀运算符:这些运算符位于其操作数之前。例如, ! $g, – 7, 这里, ! and –运算符位于操作数 $a 和 3 之前。
    • 后缀运算符:这些运算符出现在其操作数之后。例如,$y ++,这里,++运算符出现在操作数 $x 之后。
    • Circumfix运算符:这些运算符包含其操作数,如哈希构造函数和引用运算符。例如,(qq[..])
    • Postcircumfix运算符:这些运算符跟随某些特定的操作数并围绕一些操作数。例如,$hash{$y}

运算符优先级和关联性表

OperatorPrecedenceAssociativity
->Arrow OperatorLeft to Right
++, —Increment, decrementNon Associative
**ExponentRight to Left
!, +, -, ~Not, unary plus, unary minus, complementRight to Left
!=, ~=Not equal to, complement equal toLeft to Right
*, /, %Multiply, Divide, ModulusLeft to Right
<<, >>Shifting OperatorsLeft to Right
<>, <=, >=Comparison, Less than equal to, right than equal toNon Associative
&Bitwise ANDLeft to Right
|, ^Bitwise OR, EX-ORLeft to Right
&&ANDLeft to Right
||ORLeft to Right
..Range OperatorNon Associative
*=, /=, +=, -=Multiply equal to, Divide equal to, plus equal toRight to Left