You are in: > Home > Developers Guides
Perl - Scalar Variables

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:
- come up with a name, preferably a short and descriptive word preceded by the dollar sign on the left
- put the assignment operator in the middle
- put a value on the right
Some important features of variables are:
- Variables are interpolated within double quotes but not within single quotes.
- Variables can store numeric or string values.
- Perl is case sensitive.
- Backslash [\] escapes certain strings that are reserved for Perl.
- To avoid cluttering of quotes, you can use [q] or [qq] to escape single or double quotes.
$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 [
print "What is your name? "; $name =; #get input from user chomp $name; print "Hello $name!";
Related articles
- Perl - Downloading a URL with a script - [22nd Mar 2006]
- Perl - Pattern Matching (Regular Expressions) - [12th Jan 2006]
- Shell Scripting - Reading Variables From STDIN - [11th Jan 2006]
- Perl - Configuring and Starting the mod_perl Server - [06th Jan 2006]
- Perl - Installing mod_perl in Three Steps - [06th Jan 2006]
- Perl - mod_perl - [06th Jan 2006]
- Perl - Associative Arrays - [06th Jan 2006]
- Perl - Manipulating Arrays - [06th Jan 2006]
- Perl - Arrays - [06th Jan 2006]
- Perl - Operators - [06th Jan 2006]
- Perl - Scalar Variables - [06th Jan 2006]
- Shell Scripting - Introduction - [08th Dec 2005]
- Shell Scripting - Invoking Shell Scripts - [08th Dec 2005]
- Shell Scripting - Variables - [08th Dec 2005]
- Shell Scripting - Referencing Variables - [08th Dec 2005]
- Shell Scripting - Command Arguments - [08th Dec 2005]
- Shell Scripting - Language Constructs - [08th Dec 2005]
- Perl - Introduction - [06th Dec 2005]
- MySQL - Connecting to the server - [06th Dec 2005]
- MySQL - Introduction - [06th Dec 2005]







