PERL Operator

                                                                                                                         

 

PERL Operator

  • Numeric and Logic Operators:

Example of logic operators:         +, -, *, /, %, ++, –, +=, -=, *=, /=, ||, &&, ! et

**   is used for exponentiation

 

  • Concatenation : . – similar to strcat

Example :

$first_name = “Larry” ;

$last_name = “Wall” ;

$full_name = $first_name.$last_name;

print “$full_name” ;

Result: Larry Wall

 

  • Equality/ Inequality :

eq used for equal to

and ne used for not equal to

$language = “Perl” ;

if ($language == “Perl”) … # Wrong!

if ($language eq “Perl”) … #Correct

Note: Use eq / ne rather than == / != for strings

  •  Relational Operator :

1.Greater than:

Numeric :  >                               String : gt

2.Greater than or equal to:

Numeric :  >=                             String : ge

3.Less than:

Numeric :  <                                String : lt

4.Less than or equal to:

Numeric : <=                              String : le

Others Operator :

cos                cosine

sin                 sine

rand              random number between 0 and argument

length           length of argument

lc                   converts to lowercase

uc                  converts to uppercase

Type Conversion:

  • Converting strings to numbers:
    • Perl is flexible when using string types as numbers, as long as the conversion makes sense. For example, this works:

$str1 = “1”;

$num1=10-$str1;

print $num1;

Result: 9

    • So Perl can convert the string to a number if the string looks like a number.

 

  • Converting numbers to strings:
    • Perl can also convert numbers to strings when the conversion makes sense:

$num1=3;

$str1 =”yes I have “ . $num1 . ”pens” ;

print $str1;

Result: “yes I have 3 pens”

 

                                                                                                                         

 

Leave a Reply

Your email address will not be published. Required fields are marked *