A variable that, at any given time, stores only a single unit of data such as,
is called a scalar variable. Variables in Perl include a ($
) symbol followed by its name.
In Perl, a scalar can be numeric data or a string.
The following code snippet shows how to declare and initialize different scalar variables in Perl, and print them.
$item_no = 69; # An integer assignment$item_name = "Nutella"; # A string$price = 1269.00; # A floating pointprint "Item number: $item_no\n";print "Item name: $item_name\n";print "Price: $price\n";
In Perl, string scalars can be initialized with a variable name, followed by a value in double quotes (""
) or single quotes (''
) .
$doublequote = "educative";$num_string = "69";$singlequote = 'Volleyball';print "Simple string in double quote: $doublequote\n";print "String with numeric values: $num_string\n";print "String within single quotes: $singlequote\n";
Numeric scalar variables can be of multiple types:
$integer = 450; # integer scalar$negative = -230; # negative integer$floating = 786.876; # floating point$bigfloat = -5.4e-12; # big float scalar$double = 6.9; # double scalarprint "integer = $integer\n";print "negative = $negative\n";print "floating = $floating\n";print "bigfloat = $bigfloat\n";print "double = $double\n";
Free Resources