Perl - Operators:

  • Arithmetic
  • Assignment
  • Logical
  • Pattern Matching
  • String
  • Relational
  • Arithmetic Operators:

    OperatorExampleMeaning
    +$a + $bSum of $a and $b
    -$a - $bDifference of $a and $b
    *$a * $bProduct of $a times $b
    /$a / $bQuotient of $a divided by $b
    %$a % $bRemainder of $a divided by $b
    **$a ** $b$a to the power of $b

    Assignment Operators:

    OperatorExampleMeaning
    =$var = 5assign 5 to $var
    ++$var++ or ++$varincrement $var by 1 and assign to $var
    --$var-- or --$vardecrement $var by 1 and assign to $var
    +=$var += 3increase $var by 3 and assign to $var
    -=$var -= 2decrease $var by 2 and assign to $var
    .=$str .= "ing"concatenate"ing" to $str and assign to $str
    *=$var *= 4multiply $var by 4 and assign to $var
    /=$var /= 2divide $var by 2 and assign to $var
    **=$var **= 2raise $var to the second power and assign to $var
    %=$var %= 2divide $var by 2 and assign remainder to $var
    x=$str x= 20repeat $str 20 times and assign to $str

    Logical Operators:

    OperatorExampleMeaning
    &&$a && $bTrue if $a is true and $b is true
    ||$a || $bTrue if $a is true or if $b is true
    !! $aTrue if $a is not true

    Pattern Matching Operators:

    OperatorExampleMeaning
    =~ //$a =~ /pat/True if $a contains pattern "pat"
    =~ s//$a =~ s/p/rReplace occurences of 'p' with 'r' in $a
    =~ tr//$a =~ tr/a-z/A-ZTranslate to corresponding characters
    !~ //$a !~ /pat/True if $a does not contain pattern "pat"

    String Operators:

    OperatorExampleMeaning
    .$a . $bconcatenate $b to the end of $a
    x$a x $bvalue of $a strung together $b times
    substr()substr($a, $o, $l)Substring of $a at offset $o of length $l
    index()index($a, $b)Offset of string $b in string $a

    Relational Operators:

    Numeric OperatorString operatorExampleMeaning
    ==eq$str eq "Word"Equal to
    !=ne$str ne "Word"Not equal to
    >gt$var > 10Greater than
    >=ge$var >= 10Greater than or equal to
    <lt$var < 10Less than
    <=le$var <= 10Less than or equal to