Home >> perldatatypes >>Perl ScalarsThe most basic kind of variable in Perl is the scalar variable. Scalar variables hold both strings and numbers, and are remarkable in that strings and numbers are completely interchangable. For example, the statement$priority =10; sets the scalar variable $priority to 10, but you can also assign a string to exactly the same variable: $priority = 'high'; Perl also accepts numbers as strings, like this: $priority = '10'; $default = '0010'; and can still cope with arithmetic and other operations quite happily. In general variable names consists of numbers, letters and underscores, but they should not start with a number and the variable $_ is special, as we'll see later. Also, Perl is case sensitive, so $a and $A are different.
Operations and AssignmentPerl uses all the usual C arithmetic operators:
InterpolationThe following code prints apples and pears using concatenation:$a = 'apples'; $b = 'pears'; print $a.' and '.$b; It would be nicer to include only one string in the final print statement, but the line print '$a and $b'; prints literally $a and $b which isn't very helpful. Instead we can use the double quotes in place of the single quotes: print "$a and $b"; The double quotes force interpolation of any codes, including interpreting variables. This is a much nicer than our original statement. Other codes that are interpolated include special characters such as newline and tab. The code \n is a newline and \t is a tab. Perl functions
|
Perl @ARGV %ENV Perl Arrays Perl Data Types Perl Hash Perl list Perl references Perl Special Variables
|