leestretton.com

You are in: > Home > Developers Guides

Perl - Scalar Variables

Perl Camel

Scalar variables are also called simple variables.

When you name a scalar variable, after the dollar sign, the first character typically has to be a letter (underscore is okay too).

Avoid using numbers or other symbols as first character, there are many built-in variables of Perl that use symbols following a dollar sign and other conventions regarding the numbers that may not agree and result in errors. Also note that the variable names are case sensitive.

$table is not the same as $Table

Here are a few examples of acceptable scalar variable names:

$name
$DNA
$sequence_xyz
$seq1
$_apple
$_o_r_a_n_g_e
$p
$here_CNN_is_broadcasted_on_channel_42
$_123
$lesson03

In order to assign a scalar value to a variable:

Some important features of variables are:

$a = 8;
$b = 2;
$c = $a + $b;
print $c, "\n";              #will print 10
print "\$c = $c\n";       #will print $c = 10
print "$c = $c\n";        #will print 10 = 10
print '$c = $c', "\n";     #will print $c = $c
print 'the value of scalar variable c is TEN'; 

Backtics:

You can enclose UNIX or DOS system commands in backtics (`). This executes the system command, and returns its output:

$date = `date`;

print $date;

Setting Variables from the command line

Standard input or [] means the terminal entry. You can ask a question, and the user can answer. According to the answer entered, you can do different things. Here is an example script that uses users's input:

print "What is your name? ";
$name = ;   #get input from user
chomp $name;
print "Hello $name!"; 

Problem with this page? Let us know
© Lee Stretton 2005 | powered by linux powered by php 5 powered by MySQL download rss feeds powered by php 5 valid css