Slide 6.a: Perl (Practical Extraction and Report Language)
Slide 6.c: Arrays
Home

Variables and Strings


Variables
A scalar variable stores a single (scalar) value. Perl scalar names are prefixed with a dollar sign ‘$’, so for example, $x, $y, $z, $username, and $url are all examples of scalar variable names. It is not necessary to declare a variable before using it. A scalar can hold data of any type, be it a string, a number, or whatever.



 $var = ;
 print  "\$var is an integer:  $var";
 $var = "";
 print  "\$var is a string:  $var";
 $var = ;
 print  "\$var is a real number:  $var";
Strings
Scalars can be used in double-quoted strings. Double-quoted strings are subject to variable interpolation of scalar and list values, i.e., the variable values can be inserted directly into a string literal. The single-quoted strings are not subject to that.



 $number = "";
 $course = "CSci$number Programming Languages";
 print $course;


 $number = "";
 $course = 'CSci$number Programming Languages';
 print $course;
Some Double-Quoted String Representation
Construct Meaning Construct Meaning Construct Meaning
\n Newline \r Return \t Tab
\f Formfeed \b Backspace \a Bell
\e Escape \\ Backslash \" Double quote
\007 Any octal ASCII value \x7f Any hex ASCII value \cC Any “control” character