Node:String Operators, Previous:Auto-Increment and Decrement, Up:Operators



String Operators

The final set of operators that we will consider are those that operate specifically on strings. Remember, though, that we can use numbers with them, as Perl will do the conversions to strings when needed.

The string operators that you will see and use the most are . and x. The . operator is string concatenation, and the x operator is string duplication.

     use strict;
     my $greet = "Hi! ";
     my $longGreet  = $greet x 3;   # $longGreet is "Hi! Hi! Hi! "
     my $hi = $longGreet . "Paul.";  # $hi is "Hi! Hi! Hi! Paul."
     

Assignment with Operators

It should be duly noted that it is possible to concatenate, like in C, an operator onto the assignment statement to abbreviate using the left hand side as the first operand. For example,

     use strict;
     my $greet = "Hi! ";
     $greet  .= "Everyone\n";
     $greet  = $greet . "Everyone\n"; # Does the same operation
                                      # as the line above
     

This works for any simple, binary operator.