Home arrow Perl Programming arrow Page 3 - Perl: More on Data Types and Operators

Operating the Deep Seas - Perl

So, when we last left off, I left you lost in a tumultuous sea of data types, variables, and strings. Fortunately for you, you have the black Perl, the world's fastest ship and a magical compass to point you in the right direction. So let's prepare to sail, er, study, and mind the shoals.

TABLE OF CONTENTS:
  1. Perl: More on Data Types and Operators
  2. Hashes are Yummy
  3. Operating the Deep Seas
  4. Incremental/ Decremental
  5. Comparing Numbers
By: James Payne
Rating: starstarstarstarstar / 2
October 15, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Argh! Thar she blows! The great white whale of the sea -- or, more precisely, the treasure that every Perl diver seeks. In this case, though, it's not something that goes into jewelry, it's something that adds quite a bit of power to your coding. 

Operators in Perl (as in all programming languages) allow you to manipulate data. You've worked with operators ever since elementary school, albeit somewhat differently. 

Just as there are different data types, there are different operators for the different kinds of data. We will go through a bunch of them. First though, how would you like to see a gigantic table full of mind-numbing data? I thought you might.

Mathematical Operators


 

Symbol

What it Does

+

Adding

-

Subtracting

*

Multiplying

/

Dividing

%

Modulating

**

Exponentiating


The above operators work just as you think they would. If I wanted to figure out my weekly salary, I could do the follow code:


#!/usr/local/bin/perl


$hours = 40;

$wage = 20;

$total_salary = $hours * $wage;

print $total_salary

That would give me the following result:

  800

Operators that Assign Values


 

 

Symbol

What it Does

=

Assign Normally

+=

Assign and Add

-=

Assign and Subtract

*=

Assign and Multiply

/=

Assign and Divide

%=

Assign and Modulate

**=

Assign and Exponentiate

In the previous tutorial we used the = symbol to assign a value to a variable. We can use it for other things as well. For instance, let's say you are the boss's son and have just received your first paycheck. It isn't enough to buy that Porsche you desperately need to compensate for certain characteristics you lack. Well, you can always change it like this:


$pay_rate = 20;

$pay_rate+=80;

print $pay_rate;

The above example would print your pay rate as being: 100. Another way of looking at it is saying you had written this code instead:


$pay_rate = 20;

$pay_rate=$pay_rate + 80;

print $pay_rate;

You can do the same sort of thing with the other operators above.



 
 
>>> More Perl Programming Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: