Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Perl Variables: Scalars

April 26, 1999

Variables allow us to manipulate values which, as the name implies, vary over time. Variables are used to contain values, be they arithmetic (numbers) or strings (a bunch of text characters), and variables can be operated upon, meaning added, subtracted, combined, multiplied, and so on.

The most common, basic variable in Perl is known as a scalar variable, and is represented by a preceding dollar sign ($). For example, imagine that we want to use a variable to keep the current exchange rate between American and Canadian currency, and this variable will be named "exRate". In Perl we would refer to this variable as:

$exRate

We could simply assign a value to exRate:

$exRate=1.35;

We could create another variable, $USTotal, to represent a dollar amount in American currency, then calculate the value of a resulting variable, $CDNTotal, and output the results.

 
#!/usr/bin/perl

$exRate=1.35;
$USTotal=50.00;
$CDNTotal=$USTotal*$exRate;
print "$USTotal American dollars is equivalent ". 
"to $CDNTotal Canadian dollars.\n";

This simple program assigns values to two variables, and in the third line operates on them while assigning the results of that operation to a third variable. The operator used here is multiplication, represented by the asterisk (*), a typical operator in programming languages.

Notice that the scalar variable names appear within the double-quoted output string -- this may appear unusual to those familiar with other programming languages, but Perl recognizes that $USTotal and $CDNTotal are variable names and it substitutes their values in the output. The resulting output of the above program would look like:

Note the way the output strings have been broken up into multiple segments. Although we could have simply typed one long line, it would have been difficult to read within this article. To make the line more legible, the output string has been broken into portions, with a concatenation operator, the ".",between each portion. The result is exactly the same as if we typed one long line.
50 American dollars is equivalent to 67.5 Canadian dollars.

As you can see, a scalar variable holds one "piece" of information, known as a value. In other programming languages, there is strict control over the "types" of data that a value can represent. For example, a real number is an arithmetic value containing a decimal point (e.g. 25.75). An integer is a whole number (e.g. 25), while a string is a collection of alphanumeric characters (e.g. "hello 1999"). Whereas other languages may demand that you declare the type of value that a variable will contain, Perl is far less strict. In Perl, you can assign a scalar variable with any type of value you wish -- Perl will infer this value's type using its intelligence. Soon, we'll see why this is so useful.

When using scalar variables, you typically engage in one of two types of actions: operations and comparisons.

Perl Variables: Operating on Scalars

We've already seen one example of operating on some scalar variables -- recall line 3 of the previous example:

$CDNTotal=$USTotal*$exRate;

There are two operations here: the multiplication operator, represented by the asterisk (*), multiplies the value of $USTotal by the value of $exRate. The assignment operator, represented by the equal sign (=), assigns the result of the multiplication operation to the variable $CDNTotal.

Remember previously we opined that Perl uses its intelligence to infer the type of value a variable contains. For instance, when you multiply two variables, this operation only makes sense if the two variables contain numeric values. You can't, after all, multiply the value "chicken" by the value "eggs". Thus, when you use an arithmetic operator, Perl tries its hardest to use the variable's values as numbers.

Some operators, though, are not arithmetic. For example, consider the string concatenation operator, represented by the dot character (.). String concatenation is the result of squishing together two string values; for instance:

"hello" . "goodbye"
yields
"hellogoodbye"

Here we come to the important point: if you use an arithmetic operator on two values which can be seen as numbers, Perl will perform the arithmetic. If you use a string operator on the two values, Perl will treat the values as strings. Thus:

5 + 10
yields
15
5 . 10
yields
"510"

Running Perl Programs
The Perl You Need to Know
Table 1. Basic Perl Operators


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers