Scalar Variables

To store scalar data in Perl, you must use a scalar variable. In Perl, you indicate a scalar variable with a dollar sign followed by the name of the variable. The following are some examples:

$a
$total
$Date
$serial_number
$cat450

The dollar sign—called a type identifier—indicates to Perl that the variable contains scalar data. Other variable types (hashes and arrays) use a different identifier or no identifier at all (filehandles). Variable names in Perl—whether for hashes, arrays, filehandles, or scalars—must conform to the following rules:

  • Variable names can contain alphabetic (a to z, A to Z) characters, numbers, or an underscore character (_) after the type identifier. The first character of a variable name can't be a ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.