How to use scalars in Perl
A variable that, at any given time, stores only a single unit of data such as,
- an integer
- a floating-point number
- a character
- a string
- a paragraph
- an entire web page
is called a scalar variable. Variables in Perl include a ($) symbol followed by its name.
Numeric and string scalars
In Perl, a scalar can be numeric data or a string.
Code
Example 01
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";
String scalars
In Perl, string scalars can be initialized with a variable name, followed by a value in double quotes ("") or single quotes ('') .
Example 02
$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 scalars
Numeric scalar variables can be of multiple types:
- Integer (positive/negative)
- Whole numbers.
- Float/Big float.
- Double.
Example 03
$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
Copyright ©2026 Educative, Inc. All rights reserved