Logo         Community
The Company
Your Account
Contact Us
Perl 101 (part 2): Of Variables And Operators
Learn about Perl's variables, operators, and conditional expressions.

| Playing With Mud |

In the first part of this tutorial, we introduced you to the basics of Perl, Austin Powers-style. This week, we're going to get down and dirty with variables and operators, and also provide you with a brief introduction to Perl's conditional expressions.

To begin with, let's answer a very basic question for all those of you unfamiliar with programming jargon: what's a variable when it's at home?

A variable is the fundamental building block of any programming languages. Think of a variable as a container which can be used to store data; this data is used in different places in your Perl program. A variable can store both numeric and non-numeric data, and the contents of a variable can be altered during program execution. Finally, variables can be compared with each other, and you - the programmer - can write program code that performs specific actions on the basis of this comparison.

Every language has different types of variables - however, for the moment, we're going to concentrate on the simplest type, referred to in Perl as "scalar variables". A scalar variable can hold any type of value - integer, text string, floating-point number - and is usually preceded by a dollar sign.

The manner in which scalar variables are assigned values should be clear from the following example:


# a simple scalar variable
$name = "mud";

# and an example of how to use it
print ("My name is ", $name);

And here's what the output of that program looks like:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
$ My name is mud

Really?! We feel for you...

It shouldn't be too hard to see what happened here - the scalar variable $name was assigned the text value "mud", and this value was then printed to the console via the print() statement.

Although assigning values to a scalar variable is extremely simple - as you've just seen - there are a few things that you should keep in mind here:

* A scalar variable name must be preceded by a dollar [$] sign - for example, $name, $id and the like. This helps differentiate between scalar variables and arrays or hashes, which are the other types of variables supported by Perl.

* Every scalar variable name must begin with a letter, optionally followed by more letters or numbers - for example, $a, $data123, $i_am_god

* The maximum length of a scalar variable name is 255 characters; however, if you use a name that long, you need therapy!

* Case is important when referring to scalar variables - in Perl, a $cigar is definitely not a $CIGAR!

* It's always a good idea to give your variables names that make sense and are immediately recognizable - it's easy to tell what $gross_income refers to, but not that easy to identify $ginc.

* Unlike other programming languages, a scalar variable in Perl can store both integers and floating-point numbers [also known as decimals]. This added flexibility is just one of the many nice things about Perl.

Here's another program, this one illustrating how variables can be used to store and manipulate numbers.


# declare a variable
$number = 19;

print ($number, " times 1 is ", $number,"\n");
print ($number, " times 2 is ", $number * 2, "\n");
print ($number, " times 3 is ", $number * 3, "\n");
print ($number, " times 4 is ", $number * 4, "\n");
print ($number, " times 5 is ", $number * 5, "\n");

And here's what the output looks like:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
19 times 1 is 19
19 times 2 is 38
19 times 3 is 57
19 times 4 is 76
19 times 5 is 95

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

more like this print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback