Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

Operators in Perl

Perl offers a rich set of operators for various operations, including arithmetic, comparison, logical operations, and more. This tutorial will walk you through the major operators in Perl and how to use them.

1. Arithmetic Operators

These are used for mathematical operations:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder after division)
  • **: Exponentiation

2. Comparison Operators

Used for numeric comparison:

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

For string comparison:

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

3. Logical Operators

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT
  • and: Low precedence logical AND
  • or: Low precedence logical OR
  • not: Low precedence logical NOT

4. Assignment Operators

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

5. Bitwise Operators

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

6. String Operators

  • .: Concatenate strings
  • x: Repeat string

Example:

print "hello" . "world";  # helloworld
print "hello" x 3;       # hellohellohello

7. Range Operator

  • ..: Creates a range of values.

Example:

@numbers = (1..5);       # (1, 2, 3, 4, 5)
@letters = ('a'..'e');   # (a, b, c, d, e)

8. Conditional Operator

  • ? :: Ternary conditional operator

Example:

$age < 18 ? "minor" : "adult";

9. File Test Operators

These are unary operators that test attributes of a file:

  • -e: File exists
  • -z: File is empty
  • -s: File size (returns size or undef if doesn't exist)
  • -r: File is readable
  • -w: File is writable
  • -x: File is executable
  • -f: Regular file (not a directory or device file)
  • -d: Directory ... and many more.

Example:

if (-e $filename) {
    print "$filename exists.\n";
}

10. Special Operators

  • ..: Flip-flop operator in scalar context.
  • <>: Diamond operator to read from file or standard input.

Conclusion

Perl's operator set is diverse and flexible, providing tools for a variety of operations, from basic arithmetic to file tests. Familiarity with these operators is key to writing efficient and readable Perl code.

  1. Arithmetic operators in Perl:

    • Description: Arithmetic operators perform mathematical operations on numbers.
    • Example Code:
      my $sum = 5 + 3;    # Addition
      my $difference = 7 - 2;  # Subtraction
      my $product = 4 * 6;     # Multiplication
      my $quotient = 10 / 2;    # Division
      my $remainder = 17 % 4;   # Modulus
      
  2. Comparison operators in Perl:

    • Description: Comparison operators compare values and return a Boolean result.
    • Example Code:
      my $result1 = (10 == 5);   # Equal to
      my $result2 = (7 != 3);    # Not equal to
      my $result3 = (8 > 6);     # Greater than
      my $result4 = (4 < 9);     # Less than
      my $result5 = (12 >= 10);  # Greater than or equal to
      my $result6 = (2 <= 5);    # Less than or equal to
      
  3. Logical operators in Perl:

    • Description: Logical operators perform Boolean operations on values.
    • Example Code:
      my $and_result = ($condition1 && $condition2);  # Logical AND
      my $or_result = ($condition1 || $condition2);   # Logical OR
      my $not_result = !($condition);                  # Logical NOT
      
  4. Bitwise operators in Perl:

    • Description: Bitwise operators perform operations at the bit level.
    • Example Code:
      my $bitwise_and = $num1 & $num2;  # Bitwise AND
      my $bitwise_or = $num1 | $num2;   # Bitwise OR
      my $bitwise_xor = $num1 ^ $num2;  # Bitwise XOR
      my $bitwise_not = ~$num;          # Bitwise NOT
      
  5. String operators in Perl:

    • Description: String operators perform operations on strings.
    • Example Code:
      my $concatenation = $str1 . $str2;  # String concatenation
      my $repetition = $str x 3;          # String repetition
      
  6. Assignment operators in Perl:

    • Description: Assignment operators assign values to variables.
    • Example Code:
      my $num = 10;
      $num += 5;    # Addition assignment (equivalent to $num = $num + 5)
      $num -= 3;    # Subtraction assignment
      $num *= 2;    # Multiplication assignment
      $num /= 4;    # Division assignment
      
  7. Ternary operator in Perl:

    • Description: The ternary operator provides a concise way to write conditional expressions.
    • Example Code:
      my $result = ($condition) ? "True" : "False";
      
  8. Precedence of operators in Perl:

    • Description: Operators in Perl have a specific precedence, determining the order of evaluation.
    • Example Code:
      my $result = 2 + 3 * 4;  # Precedence: Multiplication has higher precedence than addition
      
  9. Overloading operators in Perl:

    • Description: Perl allows overloading certain operators for custom classes.
    • Example Code:
      package MyNumber;
      
      use overload '+' => \&add_numbers;
      
      sub new {
          my ($class, $value) = @_;
          return bless { value => $value }, $class;
      }
      
      sub add_numbers {
          my ($self, $other, $swap) = @_;
          return $swap ? $other + $self->{value} : $self->{value} + $other;
      }
      
      my $num1 = MyNumber->new(5);
      my $num2 = MyNumber->new(3);
      my $result = $num1 + $num2;