1. Home
  2. Computing & Technology
  3. Perl

Beginning Perl Tutorial - Data Types: Scalars

Playing with Scalars

By Kirk Brown, About.com

Once you've stored a value in a box and labeled it, you're ready to manipulate that data with other operators. Let's look at the simple arithmetic operators as an example:
#!/usr/local/bin/perl
$valueAdd = 1 + 1;
$valueSubtract = 4 - 1;
$valueMultiply = 2 * 2;
$valueDivide = 10 / 2;
print "$valueAdd\n";
print "$valueSubtract\n";
print "$valueMultiply\n";
print "$valueDivide\n";
When we run the above program we get the following output:
$ ./simpleProgram.pl
2
3
4
5
$
Lets take a look at another example using both text and numerical scalars:
#!/usr/local/bin/perl
$textSample = "This is a string.\n";
$numberOne = 5;
$numberTwo = 3;
print $textSample;
print "$numberOne + $numberTwo = " . ($numberOne + $numberTwo) . "\n";
print "$numberOne * $numberTwo = " . ($numberOne * $numberTwo) . "\n";
This will output:
$ ./simpleProgram.pl
This is a string.
5 + 3 = 8
5 * 3 = 15
$
I've added in one last operator in the above example - the concatenation operator, or the period. It acts as a sort of bridge between two strings to glue them together. In the example above, you can see that I use it to join the first part of the string and the newline with a small mathematical calculation.

While this may seem like a lot of information to process and take away, don't worry. We will go over more of these topics in greater detail later. For now, there are only a few important things you need to remember:

  1. A scalar is the first of 3 basic data types in Perl.
  2. A scalar is a virtual box that holds a single piece of data.
  3. You can use operators to modify the data within your variable, and access it using the $variableName you designate.
Next: Beginning Perl Tutorial - Data Types: Lists

Explore Perl

More from About.com

  1. Home
  2. Computing & Technology
  3. Perl
  4. Perl Tutorials
  5. Beginning Perl Tutorial - Data Types: Scalars

©2008 About.com, a part of The New York Times Company.

All rights reserved.