Basic
I/O
The
easiest means to get operator input to your program is using the
“diamond” operator:
$input
= <>;
The
input from the diamond operator includes a newline (\n). To get rid
of this pesky character, use either chop()
or chomp(). chop()
removes the last character of the string, while chomp()
removes any line-ending characters (defined in the
special variable $/).
If no argument is given, these functions operate on the $_
variable. To do the converse, simply use Perl’s print
function:
print
$output.”\n”;
Basic
Operators
Arithmetic
Example
Name Result
$a
+ $b Addition
Sum of $a
and
$b
$a
* $b Multiplication
Product of $a
and
$b
$a
% $b Modulus
Remainder of $a
divided
by $b
$a
** $b Exponentiation
$a
to
the power of $b
String
Example
Name Result
$a
. “string” Concatenation
String built from pieces
“$a
string” Interpolation
String incorporating the value of $a
$a
x $b Repeat
String in which $a
is
repeated $b
times
Assignment
The
basic assignment operator is “=”:
$a
= $b.
Perl
conforms to the C idiom that lvalue
operator= expression is
evaluated as: lvalue = lvalue operator
expression
So
that $a
*= $b is
equivalent to $a
= $a * $b
$a
+= $b $a = $a + $b
This
also works for the string concatenation operator: $a
.= “\n”
Autoincrement
and Autodecrement
The
autoincrement and autodecrement operators are special cases of the
assignment operators, which add or subtract 1 from the value of a
variable:
++$a,
$a++ Autoincrement
Add 1 to $a
--$a,
$a-- Autodecrement
Subtract 1 from $a
Logical
Conditions
for truth: Any string is true except for “” and “0” Any
number is true except for 0 Any reference is true Any undefined value
is false
Example
Name Result
$a
&& $b And
True if both $a
and
$b
are
true
$a
|| $b Or
$a
if
$a
is
true; $b
otherwise
!$a
Not
True if $a
is
not true
$a
and $b And
True if both $a
and
$b
are
true
$a
or $b Or
$a
if
$a
is
true; $b
otherwise
not
$a Not
True if $a
is
not true
Logical
operators are often used to “short circuit” expressions, as in:
open(FILE,”<
input.dat”) or die “Can’t open file”;
Comparison
Comparison
Numeric String Result
Equal
==
eq True
if $a equal to $b
Not
equal !=
ne True
if $a not equal to $b
Less
than <
lt True
if $a less than $b
Greater
than >
gt True
if $a greater than $b
Less
than or equal <=
le True
if $a not greater than $b
Comparison
<=>
cmp 0
if $a and $b equal
1
if $a greater
-1
if $b greater
Operator
Precedence
Perl
operators have the following precedence, listed from the highest to
the lowest, where operators at the same precedence level resolve
according to associativity:
Associativity
Operators Description
Left
Terms and
list
operators
Left
->
Infix
dereference operator
++
--
Auto-increment
Auto-decrement
Right
Right
Right
\
!
~
+
-
Reference
to an object (unary)
Unary
negation, bitwise complement
Unary
plus, minus
Left
Left
=~
!~
Binds
scalar to a match pattern
Same,
but negates the result
Left
* /
% x Multiplication,
Division, Modulo, Repeat
Left
+ -
. Addition,
Subtraction, Concatenation
Left
>>
<< Bitwise
shift right, left
<
> <= >=
lt
gt le ge
Numerical
relational operators
String
relational operators
==
!= <=>
eq
ne cmp
Numerical
comparison operators
String
comparison operators
Left
&
Bitwise
AND
Left
| ^
Bitwise
OR, Exclusive OR
Left
&&
Logical
AND
Left
||
Logical
OR
..
In
scalar context, range operator
In
array context, enumeration
Right
?:
Conditional
(if ? then : else) operator
Right
=
+= -= etc Assignment
operators
Left
,
=>
Comma
operator, also list element
separator
Same,
enforces left operand to be string
Right
not
Low
precedence logical NOT
Right
and
Low
precedence logical AND
Right
or
xor Low
precedence logical OR
Parentheses
can be used to group an expression into a term. A list consists of
expressions, variables, or lists, separated by commas. An array
variable or an array slice many always be used instead of a list.
No comments :
Post a Comment