Contents / Previous / Next


Operators

** Exponentiation
+ - * / Addition, subtraction, multiplication, division
% Modulo division
& | ^ Bitwise AND, bitwise OR, bitwise exclusive OR
>> << Bitwise shift right, bitwise shift left
|| && Logical OR, logical AND
. Concatenation of two strings
x Returns a string or array consisting of the left operand (an array or a string) repeated the number of times specified by the right operand
All of the above operators also have an assignment operator, e.g., .=
-> Dereference operator
\ Reference (unary)
! ~ Negation (unary), bitwise complement (unary)
++ -- Auto-increment (magical on strings), auto-decrement
== != Numeric equality, inequality
eq ne String equality, inequality
< > Numeric less than, greater than
lt gt String less than, greater than
<= >= Numeric less (greater) than or equal to
le ge String less (greater) than or equal to
<=> cmp Numeric (string) compare. Returns -1, 0, 1.
=~ !~ Search pattern, substitution, or translation (negated)
.. Range (scalar context) or enumeration (array context)
?: Alternation (if-then-else) operator
, Comma operator, also list element separator. You can also use =>.
not Low-precedence negation
and Low-precedence AND
or xor Low-precedence OR, exclusive OR


Operations on Strings (See also: I)

. Concatenation of two strings.
.= The concatenation assignment operator.
eq String equality (== is numeric equality). For a mnemonic just use "eq" as a string.
ne String inequality (!= is numeric inequality).
lt String less than.
gt String greater than.
le String less than or equal.
ge String greater than or equal.
cmp String comparison, returning -1, 0, or 1.
<=> Numeric comparison, returning -1, 0, or 1.
=~ Regular expression match.
!~ Regular expression non-match, like =~ except the return value is negated.
x The repetition operator. Returns a string consisting of the left operand repeated the number of times specified by the right operand. (In an array context, if the left operand is a list in parens, it repeats the list.)

Examples:

$sa = "hallo ";
$sb = "you!";

$sc = $sa . $sb;
print "$sc\n";

$sc = $sa x 3 . $sb;
print "$sc\n";

$sc = $sa le $sb;
print "$sc\n";


String Functions

chomp LIST
Removes line endings from all elements of the list; returns the (total) number of characters removed.

chop LIST
Chops off the last character on all elements of the list; returns the last chopped character.

crypt PLAINTEXT, SALT
Encrypts a string.

eval EXPR
EXPR is parsed and executed as if it were a Perl program. The value returned is the value of the last expression evaluated. If there is a syntax error or runtime error, an undefined string is returned by eval, and $@ is set to the error message.

index STR, SUBSTR [ , OFFSET ]
Returns the position of SUBSTR in STR at or after OFFSET. If the substring is not found, returns -1 (but see $[ in section Special Variables).

length EXPR
Returns the length in characters of the value of EXPR.

lc EXPR
Returns a lowercase version of EXPR.

lcfirst EXPR
Returns EXPR with the first character lowercase.

quotemeta EXPR
Returns EXPR with all regular expression metacharacters quoted.

rindex STR, SUBSTR [ , OFFSET ]
Returns the position of the last SUBSTR in STR at or before OFFSET.

substr EXPR, OFFSET [ , LEN ]
Extracts a substring of length LEN out of EXPR and returns it. If OFFSET is negative, counts from the end of the string. May be assigned to.

uc EXPR
Returns an uppercased version of EXPR.

ucfirst EXPR
Returns EXPR with the first character uppercased.


Examples:

$s = "this line\n\n";
print "$s  x-lines further down";
print "\n\n\n";

chomp $s;
print "$s x-lines further down";
print "\n\n\n";

chomp $s; chomp $s; chomp $s;
print "$s x-lines further down";
print "\n\n\n";
Output:
this line

 x-lines further down


this line
 x-lines further down


this line x-lines further down


Quote and Quotelike Operators

While we usually think of quotes as literal values, in Perl they function as operators, providing various kinds of interpolating and pattern matching capabilities. Perl provides customary quote characters for these behaviors, but also provides a way for you to choose your quote character for any of them. In the following table, a {} represents any pair of delimiters you choose. Non-bracketing delimiters use the same character before and after, but the 4 sorts of brackets (round, angle, square, curly) will all nest. Customary Generic Meaning Interpolates '' q{} Literal no "" qq{} Literal yes `` qx{} Command yes qw{} Word list no // m{} Pattern match yes s{}{} Substitution yes tr{}{} Translation no Examples:
@days[3..5]    = qw/Wed Thu Fri/;


Operations on Arrays (and Lists) (See also: I) (Arrays of arrays and 2D arrays ,local backup: perllol.html)

Array and list functions:
chomp LIST
Removes line endings from all elements of the list; returns the (total) number of characters removed.

chop LIST
Chops off the last character on all elements of the list; returns the last chopped character.

sort(LIST)
return a new list, the sorted from LIST

reverse(LIST)
return a new list, the reverse of LIST

join(EXPR,LIST)
return a string formed by concatenating each element of LIST joined by EXPR

split(PATTERN,EXPR)
return a list formed from each substring of EXPR bordered by PATTERN.

Array functions:
push(@ARRAY,LIST)
add LIST to the end of @ARRAY

pop(@ARRAY)
remove and return the last element of @ARRAY

unshift(@ARRAY,LIST)
add LIST to the front of @ARRAY

shift(@ARRAY)
remove and return the first element of @ARRAY

scalar(@ARRAY)
return the number of elements in the array


Examples:

@animals = ("camel", "llama", "owl");

push( @animals, ("you","me\n","next line") );
print "@animals";
print "\n";

chomp( @animals );
print "@animals";
Output:
camel llama owl you me
 next line
camel llama owl you me next line


Operations on Hashes (Associative Array)

keys(%ARRAY)
Return a list of all the keys in %ARRAY. The list is "unordered" - it depends on the hash function used internally.

values(%ARRAY)
Return a list of all the values in %ARRAY

each(%ARRAY)
Each time this is called on an %ARRAY, it will return a 2 element list consisting of the next key/value pair in the array.

delete($ARRAY{KEY})
Remove the pair associated with KEY from ARRAY.


Examples:

%animals = ( "camel" => "yellow", "llama" => "sand", "owl" => "white" ); #print $animals{"camel"}; #print "\n\n\n"; @k = keys(%animals); @v = values(%animals); print "@k\n@v"; print "\n\n"; delete ( @animals{"camel"} ); @k = keys(%animals); @v = values(%animals); print "@k\n@v"; print "\n\n"; Output:
camel owl llama
yellow white sand

owl llama
white sand


See also: man perlop