|
Perl : Scalar variables A Scalar variable holds 1 single piece of data. They can hold both strings and numbers and are remarkable in that strings and numbers are completely interchangable. For example, the statement
$priority = 9;
sets the scalar variable
$priority = 'high';
Perl also accepts numbers as strings and can still cope with arithmetic and other operations quite hapily, like this:
$priority = '9';
$default = '0009';
In general variable names consist of numbers, letters, and underscores, but should never start with an underscore ( Perl uses all the usual C arithmetic operators:
$a = 1 + 2; # Add 1 and 2 and store in $aand for strings Perl has the following among others:
$a = $b . $c; # Concatenate $b and $cTo assign values Perl includes:
$a = $b; # Assign $b to $aNote that when Perl assigns a value with Observe the following code: Example:
Output:
apples and pears
It would be nicer to include only one string in the final print statement, try this: Example:
The single quote will print literally which isn't very helpful. Instead we use double quotes. Example:
Double quotes force interpolation of any codes, include interpreting variables. This is much nicer and easier
to read then our original statement. Other codes that are interpolated include special characters such as newline and tab.
The code Published by: Thomas Penwell Initially published on: June 16, 2011 Article last modified on: Thursday, January 26, 2012. |
|||||||||||

