PERL TUTORIAL PART 2 – ELECDUDE
We hope you've gained the basic PERL knowledge from our
Tutorial Part 1. In this part, let's go through about the PERL's basic input
output, operators and its examples.
Click here to goto Tutorial Part 1.
BASIC I/O:
Input from STDIN
Reading from standard input is easy, via the Perl file handle called STDIN. It returns the entire line
content into a scalar variable.
$a = <STDIN>; #the
contents of input STDIN line is stored in $a
To store many lines in the variable, the array variable is used.
@arr = <STDIN>; #the contents of each input
STDIN line is stored in each element of @arr
Note that STDIN always returns along with the NEW LINE character ie \n.
To remove the new line, chomp()
function is used.
$a = <STDIN>; #the
contents of input STDIN line with \n
Chomp($a); #the new line is removed.
Output to STDOUT
Sending values to Standard Output is also easy, via the Perl file handle
called STDOUT. The keyword used for
this is PRINT/PRINTF
print "Hello!!";
print ("Hello!!");
print "Hell","o!!";
printf("%s","Hello!!"); #just like C language.
All the above statement displays the same result. Arithmetic operations
are also performed in the print statements.
$a=4;
print $a+$a; #prints 8
print ($a," Hello!!"); #prints
4 Hello!!
printf("%s-%d","Hello!!",$a*3); #just like C language.
prints Hello!!-12
Often printf statement is used for printing formatted text.
OPERATORS:
Perl language supports many
operator types but following is a list of important and most frequently used
operators:
·
Arithmetic Operators
·
Equality Operators
·
Logical Operators
·
Assignment Operators
·
Bitwise Operators
·
Logical Operators
·
Quote-like Operators
·
Miscellaneous Operators
Arithmetic
Operators
Operator
Description
+
Addition - Adds values on either side of the
operator
-
Subtraction - Subtracts right hand operand
from left hand operand
*
Multiplication - Multiplies values on either
side of the operator
/
Division - Divides left hand operand by right
hand operand
%
Modulus - Divides left hand operand by right
hand operand and returns remainder
**
Exponent - Performs exponential (power)
calculation on operators
Equality/relational
Operators
Operator
Description
==
Checks if the value of two operands are equal
or not, if yes then condition becomes true.
!=
Checks if the value of two operands are equal
or not, if values are not equal then condition becomes true.
<=>
Checks if the value of two operands are equal
or not, and returns -1, 0, or 1 depending on whether the left argument is
numerically less than, equal to, or greater than the right argument.
>
Checks if the value of left operand is greater
than the value of right operand, if yes then condition becomes true.
<
Checks if the value of left operand is less
than the value of right operand, if yes then condition becomes true.
>=
Checks if the value of left operand is greater
than or equal to the value of right operand, if yes then condition becomes
true.
<=
Checks if the value of left operand is less
than or equal to the value of right operand, if yes then condition becomes
true.
Equality
Operators for Strings
Operator
Description
lt
Returns true if the left argument is
stringwise less than the right argument.
gt
Returns true if the left argument is
stringwise greater than the right argument.
le
Returns true if the left argument is
stringwise less than or equal to the right argument.
ge
Returns true if the left argument is
stringwise greater than or equal to the right argument.
eq
Returns true if the left argument is
stringwise equal to the right argument.
ne
Returns true if the left argument is
stringwise not equal to the right argument.
cmp
Returns -1, 0, or 1 depending on whether the
left argument is stringwise less than, equal to, or greater than the right
argument.
Assignment
Operators
Operator
Description
=
Simple assignment operator, Assigns values
from right side operands to left side operand
+=
Add AND assignment operator, It adds right
operand to the left operand and assign the result to left operand
-=
Subtract AND assignment operator, It subtracts
right operand from the left operand and assign the result to left operand
*=
Multiply AND assignment operator, It
multiplies right operand with the left operand and assign the result to left
operand
/=
Divide AND assignment operator, It divides
left operand with the right operand and assign the result to left operand
%=
Modulus AND assignment operator, It takes
modulus using two operands and assign the result to left operand
**=
Exponent AND assignment operator, Performs
exponential (power) calculation on operators and assign value to the left
operand
Bitwise
Operators
Operator
Description
&
Binary AND Operator copies a bit to the result
if it exists in both operands.
|
Binary OR Operator copies a bit if it exists
in eather operand.
^
Binary XOR Operator copies the bit if it is
set in one operand but not both.
~
Binary Ones Complement Operator is unary and
has the efect of 'flipping' bits.
<<
Binary Left Shift Operator. The left operands
value is moved left by the number of bits specified by the right operand.
>>
Binary Right Shift Operator. The left operands
value is moved right by the number of bits specified by the right operand.
Logical
Operators
Operator
Description
and
Called Logical AND operator. If both the
operands are true then then condition becomes true.
&&
C-style Logical AND operator copies a bit to
the result if it exists in both operands.
or
Called Logical OR Operator. If any of the two
operands are non zero then then condition becomes true.
||
C-style Logical OR operator copies a bit if it
exists in eather operand.
not
Called Logical NOT Operator. Use to reverses
the logical state of its operand. If a condition is true then Logical NOT
operator will make false.
Miscellaneous
Operators
Operator
Description
.
Binary operator dot (.) concatenates two
strings.
x
The repetition operator x returns a string
consisting of the left operand repeated the number of times specified by the
right operand.
..
The range operator .. returns a list of values
counting (up by ones) from the left value to the right value
++
Auto Increment operator increases integer
value by one
--
Auto Decrement operator decreases integer
value by one
->
The arrow operator is mostly used in
dereferencing a method or variable from an object or a class name
Quote-like
Operators
Operator
Description
q{ }
Encloses a string with-in single quotes
qw{ }
Encloses a string with-in double quotes
qq{ }
We welcome your valuable comments and suggestion.
It helps us to do better in future.
Thank you.
Arithmetic
Operators
|
|
Operator
|
Description
|
+
|
Addition - Adds values on either side of the
operator
|
-
|
Subtraction - Subtracts right hand operand
from left hand operand
|
*
|
Multiplication - Multiplies values on either
side of the operator
|
/
|
Division - Divides left hand operand by right
hand operand
|
%
|
Modulus - Divides left hand operand by right
hand operand and returns remainder
|
**
|
Exponent - Performs exponential (power)
calculation on operators
|
Equality/relational
Operators
|
|
Operator
|
Description
|
==
|
Checks if the value of two operands are equal
or not, if yes then condition becomes true.
|
!=
|
Checks if the value of two operands are equal
or not, if values are not equal then condition becomes true.
|
<=>
|
Checks if the value of two operands are equal
or not, and returns -1, 0, or 1 depending on whether the left argument is
numerically less than, equal to, or greater than the right argument.
|
>
|
Checks if the value of left operand is greater
than the value of right operand, if yes then condition becomes true.
|
<
|
Checks if the value of left operand is less
than the value of right operand, if yes then condition becomes true.
|
>=
|
Checks if the value of left operand is greater
than or equal to the value of right operand, if yes then condition becomes
true.
|
<=
|
Checks if the value of left operand is less
than or equal to the value of right operand, if yes then condition becomes
true.
|
Equality
Operators for Strings
|
|
Operator
|
Description
|
lt
|
Returns true if the left argument is
stringwise less than the right argument.
|
gt
|
Returns true if the left argument is
stringwise greater than the right argument.
|
le
|
Returns true if the left argument is
stringwise less than or equal to the right argument.
|
ge
|
Returns true if the left argument is
stringwise greater than or equal to the right argument.
|
eq
|
Returns true if the left argument is
stringwise equal to the right argument.
|
ne
|
Returns true if the left argument is
stringwise not equal to the right argument.
|
cmp
|
Returns -1, 0, or 1 depending on whether the
left argument is stringwise less than, equal to, or greater than the right
argument.
|
Assignment
Operators
|
|
Operator
|
Description
|
=
|
Simple assignment operator, Assigns values
from right side operands to left side operand
|
+=
|
Add AND assignment operator, It adds right
operand to the left operand and assign the result to left operand
|
-=
|
Subtract AND assignment operator, It subtracts
right operand from the left operand and assign the result to left operand
|
*=
|
Multiply AND assignment operator, It
multiplies right operand with the left operand and assign the result to left
operand
|
/=
|
Divide AND assignment operator, It divides
left operand with the right operand and assign the result to left operand
|
%=
|
Modulus AND assignment operator, It takes
modulus using two operands and assign the result to left operand
|
**=
|
Exponent AND assignment operator, Performs
exponential (power) calculation on operators and assign value to the left
operand
|
Bitwise
Operators
|
|
Operator
|
Description
|
&
|
Binary AND Operator copies a bit to the result
if it exists in both operands.
|
|
|
Binary OR Operator copies a bit if it exists
in eather operand.
|
^
|
Binary XOR Operator copies the bit if it is
set in one operand but not both.
|
~
|
Binary Ones Complement Operator is unary and
has the efect of 'flipping' bits.
|
<<
|
Binary Left Shift Operator. The left operands
value is moved left by the number of bits specified by the right operand.
|
>>
|
Binary Right Shift Operator. The left operands
value is moved right by the number of bits specified by the right operand.
|
Logical
Operators
|
|
Operator
|
Description
|
and
|
Called Logical AND operator. If both the
operands are true then then condition becomes true.
|
&&
|
C-style Logical AND operator copies a bit to
the result if it exists in both operands.
|
or
|
Called Logical OR Operator. If any of the two
operands are non zero then then condition becomes true.
|
||
|
C-style Logical OR operator copies a bit if it
exists in eather operand.
|
not
|
Called Logical NOT Operator. Use to reverses
the logical state of its operand. If a condition is true then Logical NOT
operator will make false.
|
Miscellaneous
Operators
|
|
Operator
|
Description
|
.
|
Binary operator dot (.) concatenates two
strings.
|
x
|
The repetition operator x returns a string
consisting of the left operand repeated the number of times specified by the
right operand.
|
..
|
The range operator .. returns a list of values
counting (up by ones) from the left value to the right value
|
++
|
Auto Increment operator increases integer
value by one
|
--
|
Auto Decrement operator decreases integer
value by one
|
->
|
The arrow operator is mostly used in
dereferencing a method or variable from an object or a class name
|
Quote-like
Operators
|
|
Operator
|
Description
|
q{ }
|
Encloses a string with-in single quotes
|
qw{ }
|
Encloses a string with-in double quotes
|
qq{ }
|
0 comments:
Post a Comment