Home arrow Perl Programming arrow Page 3 - Perl 101 (Part 2) - Of Variables And Operators

2 + 2 ... - Perl

Now that you've got the basics of the language down, this secondarticle in the series teaches you about Perl's variables and operators, andalso introduces you to conditional expressions.

  1. Perl 101 (Part 2) - Of Variables And Operators
  2. Q
  3. 2 2 ...
  4. ... Or Two Plus Two
  5. Comparing Apples And Oranges
  6. Decisions! Decisions!
  7. Handling The Gray Areas
  8. Miscellaneous Notes
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
June 01, 2000

print this article


As you've already seen in last time's lesson, Perl comes with all the standard arithmetic operators - addition [+], subtraction [-], division [/] and multiplication [*] - and quite a few non-standard ones. Here's an example which demonstrates the important ones:
# get a number
print "Gimme a number! ";
$alpha = <STDIN>;
# get another number
print "Gimme another number! ";
$beta = <STDIN>;
# process input
# standard stuff
$sum = $alpha + $beta;
$difference = $alpha - $beta;
$product = $alpha * $beta;
$quotient = $alpha / $beta;
# non-standard stuff
$remainder = $alpha % $beta;
$exponent = $alpha ** $beta;
# display the result
print "Sum: $sum\n";
print "Difference: $difference\n";
print "Product: $product\n";
print "Quotient from division: $quotient\n";
print "Remainder from division: $remainder\n";
print "Exponent: $exponent\n";

As with all other programming languages, division and multiplication take precedence over addition and subtraction, although parentheses can be used to give a particular operation greater precedence. For example,
print(10 + 2 * 4);

returns 18, while
print((10 + 2) * 4);

returns 48.

In addition to these operators, Perl also comes with the very useful auto-increment [++] and auto-decrement [--] operators, which you'll see a lot of in the next lesson. For the moment, all you need to know is that the auto-increment operator increments the value of the variable to which it is applied by 1, while the auto-decrement operator does exactly the same thing, but in the opposite direction. Here's an example:
# initial value
$a = 7;
print("Initial value: ", $a, "\n");
# increment and display
print("After increment: ", $a, "\n");

>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: