Introductory Perl - Variables 
2/5 

Variables, on the most basic level, are just ways to store data. They allocate a space in memory and give it a name so that it can be referenced later. The most simple use of a variable is to store a value so that you can use it later without having to retype the entire thing. Since variable names can be as short as one letter, this can save a lot of time if you need to use long sentences multiple times.
There are many ways to store data, though, as you will see below. Don't worry though, because Perl is much less fussy about variable types than other languages (like C) and will often just make things work by converting between them as needed.

Here are links to some especially important components of your briefing:
Starting Perl
Variables
Control Structures
Subroutines
More...

Q
U
I
C
K

L
I
N
K
S
  Scalars

A scalar is the name given to a single value. An easy way to think about a scalar is as name for a value that works just like a name for a person. These variables can hold values as small as a single letter or as long as memory allows, just like people come in various shapes and sizes. No matter what the variable is assigned, though, there's only one of them.
Scalars are named beginning with a $, so if I wanted to assign the value grouchy to the variable current_emotion (you should avoid trying to make weird variable names with things like spaces and stick to just the alphabet (and maybe numbers if necessary)) then print it, it would look like this:
CodeResultDownload
#!/usr/local/bin/perl
$current_emotion = "grouchy";
print $current_emotion;
grouchy


grouchy.pl

If I later change moods, I can also change the value stored by simply assigning to it again, so extending the previous example:
CodeResultDownload
#!/usr/local/bin/perl
$current_emotion = "grouchy";
print $current_emotion;
$current_emotion = "angry";
$current_emotion = "happy";
print $current_emotion;
grouchyhappy


grouchy1.pl

Which is very difficult to read because it's all squished together. Inserting line breaks (with \n) easily makes the output more legible:
CodeResultDownload
#!/usr/local/bin/perl
$current_emotion = "grouchy\n";
print $current_emotion;
$current_emotion = "angry\n";
$current_emotion = "happy\n";
print $current_emotion;
grouchy
happy


grouchy2.pl

That makes it readable, but there's still the problem that it totally ignores the anger phase of the subject's emotional rollercoaster. To do that, I could just tack my new feeling onto the old ones, to create a sort of composite feeling. This is still one item; even though it's made up of more than one word, it's still stored in just one place -- $current_emotion. The . operator concatenates (adds) two strings together. So compounding the emotions would look like:
CodeResultDownload
#!/usr/local/bin/perl
$current_emotion = "grouchy-";
$current_emotion = $current_emotion . "angry-";
$current_emotion = $current_emotion . "happy\n";
print $current_emotion;
grouchy-angry-happy


grouchy3.pl

a flock of camels   Arrays

An array is a collection of scalar values. It's like a list of the people that you want to invite to a party. It's a rather silly (or nerdy, depending on your perspective) party so you refer to each guest by their position in your list rather than by their name. Operations like sorting in perl are quite simple to accomplish with lists. They can also be treated like other objects (if you're familiar with CSE 143's abstract data types, you might get excited about this) so operations like adding to them, or getting the first item are also easy (as opposed to languages like C++ where there is great pain involved in even the simplest array operation).
If I had a herd of camels and wanted to keep track of them, I might make a list of them. Since arrays are named with @ at the beginning, I would call the list @camel_herd. If I wanted to initialize, sort, and print it, it would look like this:
CodeResultDownload
#!/usr/local/bin/perl
@camel_herd = ("black one","brown one","Freda","Cletus","gray one");
@camel_herd = sort @camel_herd;
print @camel_herd;
CletusFredablack onebrown onegray one


herd.pl

The parentheses are there to make sure nothing gets left off when assigning the list. Commas work just like in English to separate listed elements.
The output isn't particularly pretty, but that can be attributed to the fact that Perl's print statement needs a little bit more information about how to print an array than just it's name. The easiest way to do this is to surround the name with quotes, which forces the interpreter to place spaces between each of the values. The modified code is as follows:
CodeResultDownload
#!/usr/local/bin/perl
@camel_herd = ("black one","brown one","Freda","Cletus","gray one");
@camel_herd = sort @camel_herd;
print "@camel_herd";
Cletus Freda black one brown one gray one


herd1.pl

If even that isn't enough satisfaction, the conversion from array to scalar gets a little more complicated. The join statement controls how an array is converted to a scalar. Since it's only useful in this case to print, it can feed it's results directly to print. The code to print each camel's name on a new line would look like this:
CodeResultDownload
#!/usr/local/bin/perl
@camel_herd = ("black one","brown one","Freda","Cletus","gray one");
@camel_herd = sort @camel_herd;
print join "\n",@camel_herd;
Cletus
Freda
black one
brown one
gray one


herd2.pl