Scalars in Perl

The Article

Scalars are the most fundamental variables or storage types used in Perl. Because of this, these are used more often than all the rest combined. Scalars hold on piece of information; it can hold numbers, a word or a phrase. A scalar can contain a full page of text if that’s what you wanted to do, but it only ones one piece of information.

$scalar = “”;

Scalars are defined using the symbol $ before it’s name. For example $name, $state, $color, etc. are all valid names. Variable names should always start with a letter but you may use numbers and underscores inside them ($name3 or $pizza_cheese).

Setting up your first scalar

To initialize any variable in perl, we use my before we call it’s name. We only my the variable the first time it is used within the script, each additional time you just call $scalar.

my $name = “sulfericacid”;

Here we are making the new scalar $name. “name” is the actual name of our variable, again this can be pretty much anything as long as it starts with a letter. We are using my before the variable name since it wasn’t made before this and we are assigning the value “sulfericacid” to $name.

Instead of having to type out “sulfericacid” in your script wherever you want to place it, you can just use $name instead. The reason behind variables is to store re-usable content so you don’t constantly have to type it out.

If you wanted to setup a scalar containing your favorite quote, you would do something like:

my $quote = “To know me, know yourself”;

Now $quote contains our favorite quote so we don’t have to continually type it out whenver we want to use it within our script.

Printing a scalar

Of the three variables you’ll be learning, scalars are the easiest in terms of creation and printing.

print $scalar;

In Perl, we use the command ‘print’ to display something to the screen. It’s the same thing as ‘echo’ in the PHP language. You call print $scalar; and whatever was contained within the scalar variable will be printed to the screen.

For example, we’ll create a scalar and store our pet’s name inside and print it to screen:

my $pet = “molly”;
print $pet;

We setup $pet and printed it to the screen. You should now see “molly” on the screen if you run the script.

Deleting a scalar value

Generally you don’t wory about deleting the contents of a scalar. If you don’t want to print it to screen or manipulate it into the rest of your program, just don’t use the variable anymore. If you truly wanted to delete it, you could assign it’s value to a null value “”.

$pet = “”;

Now pet doesn’t contain anything, it’s just empty.

Copying a scalar

If you had a variable setup and you wanted to make a different copy of it, you can set the scalar value to another scalar itself.

my $house = “red”;
my $car = “$house”;

We made the scalar $house with the value of red. We then made a new variable $car and assigned it to the value of $house. So if you were print $car; , you would get “red” on the screen.

Doing this will put all the contents of one variable into the other, incase you wanted to make a backup.

Author: Syperder Co
I waltzed into the Web Design community as a professional when I was just 15 years of age founding SpyderWebDesigns. Through the years my interests shifted from web development to backbone and user interaction.

In 2000 Sulfericacid.com was born. The world’s largest free and 100% ad-free web site where you could use and download 24 Perl and CGI script along with tutorials without limits or restrictions. January 2005 the site was renamed to SpyderScripts.com as a subsidy of SpyderCo.

In 2001 I also founded an SEO company SpyderSubmission.com. We’ve helped nearly 2300 web sites achieve higher rankings than they ever could have imagined since our launch four years ago.

On a more personal note, I’ve attained 28 certifications from BrainBench.com and about 40 certifications in total from all resources. One of these is a near Masters in Perl which ranks second highest test score in the state and 17th throughout the country.

I have a Perl Abbot status on PerlMonks.org working on getting my Perl Saint status this fall.