Perl Operators

Perl Operators

Perl provides a rich set of operators that can be used to manipulate variables and their values. Here's a quick overview of some of the most commonly used ones:

1. Arithmetic Operators:

Arithmetic operators are used to perform mathematical operations:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulus: %
  • Exponentiation: **

Example:

my $sum = 5 + 10; # 15 

2. Comparison Operators:

Comparison operators are used to compare two values:

  • Equal: ==
  • Not equal: !=
  • Greater than: >
  • Less than: <
  • Greater than or equal to: >=
  • Less than or equal to: <=

Example:

if (5 == 10) { print "Equal\n"; } else { print "Not equal\n"; } 

3. String Comparison Operators:

String comparison operators are used to compare two strings:

  • Equal: eq
  • Not equal: ne
  • Greater than: gt
  • Less than: lt
  • Greater than or equal to: ge
  • Less than or equal to: le

Example:

if ("hello" eq "world") { print "Equal\n"; } else { print "Not equal\n"; } 

4. Assignment Operators:

Assignment operators are used to assign values to variables:

  • Assign: =
  • Add and assign: +=
  • Subtract and assign: -=
  • Multiply and assign: *=
  • Divide and assign: /=
  • Modulus and assign: %=

Example:

my $x = 10; $x += 5; # $x is now 15 

5. Logical Operators:

Logical operators are used to combine conditional statements:

  • And: &&
  • Or: ||
  • Not: !

Example:

if (5 > 10 && 10 > 5) { print "True\n"; } else { print "False\n"; } 

6. Bitwise Operators:

Bitwise operators act on bits and perform bit by bit operation:

  • Bitwise AND: &
  • Bitwise OR: |
  • Bitwise XOR: ^
  • Bitwise NOT: ~
  • Left shift: <<
  • Right shift: >>

Example:

my $x = 2; # Binary: 10 my $y = 3; # Binary: 11 print $x & $y; # Binary: 10, Decimal: 2 

7. String Operators:

Perl provides special operators to manipulate strings:

  • Concatenation: .
  • Repetition: x

Example:

my $str = "Hello" . " World"; # "Hello World" 

Remember, Perl operators have a precedence order which determines the sequence in which operators are evaluated. Operators with higher precedence are evaluated before operators with lower precedence.


More Tags

jmockit phpoffice bitstring iphone-web-app unmount nexus3 formsy-material-ui android-framelayout pyscripter dyld

More Programming Guides

Other Guides

More Programming Examples