Scalars in Perl (2021-05-01)

Languages > Perl

Scalar values

Scalars are the atomic data type of Perl. They can be strings or numbers.

Note that since Perl is a weakly-typed language, a scalar variable can contain numbers and strings indifferently.


There is two string types, interpreted delimited with double-quotes, and non-interpreted delimited with simple-quotes:

"\tHello World!\n" #The \t will be replaced by a tabulation, and the \n by a line break.
'\tHello World\n' #The two \, the t and the n are considered as independent characters.
"Hello $var" #The variable $var will be replaced by its content in the string.
"${var}th" #If immediately followed by text the variable name need to be surrounded with {}.
"\"\$\@\\" #In an interpreted string, the ", $, @, and \ need to be escaped with a \ to be considered as independent characters.
'\'' #In a non-interpreted string, the ' need to be escaped with a \ to be considered as a character of the string.
'\\' #Same for a \.


5e7 #Scientific notation is allowed.



A variable does not need to be declared (unless the ‘use strict’ pragma is set), using it create it. Once created, variables are reachable in the current block of code and its sub-blocks.

my $var; #If not initialized, a variable has the special value undef.
my $name = 'Blaise';
my $msg = "Hello $name";

The undef value

my $var; #$var value is undef.
$var = undef; #undef can be assigned to a variable.
undef($var); #Equivalent to the previous line.
defined($var); #Return true if $var's value is not undef.
$var != undef; #Does not test the same thing that the defined() function because undef is 'false' in a test.

Scalar operations

Numerical operations

my $foo;

# Classics operation:
$foo = 2 + 3;
$foo = 2 - 3;
$foo = 2 * 3;
$foo = 2 / 3; #$foo == 0.666666.
$foo = int(2 / 3); #For integer division.
$foo = 2 % 3; #$foo == 2.
$foo = 2**4; #foo == 16.

# Operations and affectations:
$foo += 2;
$foo -= 3;
$foo *= 4;
$foo /= 2;
$foo %= 3;

# Increment and decrement:
$foo = 1;
my $bar = 1 + $foo++; #$bar == 2 and $foo == 2;
$bar = 1 + ++$foo; #$bar == 4 and $foo == 3;
$bar = 1 + --$foo; #$bar == 3 and $foo == 2;
$bar = 1 + $foo--; #$bar == 3 and $foo == 1;

# Functions:
sin($foo); #Sinus of $foo.
cos($foo); #Cosinus of $foo.
exp($foo); #e to the power of $foo.
log($foo); #Logarithm of $foo in e base.
abs($foo); #Absolute value of $foo.
sqrt($foo); #Square root of $foo.

Numerical context specificities

my $foo = "12" + "3"; #In numerical context, strings representing numbers will be converted without any problem. So $foo == 15.
$foo += "4th"; #Strings starting with a number will be converted to this number, but if 'use warnings' is set a warning will be printed. $foo == 19.
$foo += "Perl" * undef; #Other scalar values, including undef, will be converted to 0. $foo == 19.

String operations

# Operators
my $foo = "Hello"." world"; #$foo == "Hello world".
$foo .= "!"; #$foo == "Hello world!".
$foo = "Hey! " x 2; #$foo == "Hey! Hey! ".
$foo x= 2; #$foo == "Hey! Hey! Hey! Hey! ".

# Functions
length("Hello\n"); #The length of the string, here 6.
length('Hello\n'); #Non-interpreted string so its length is 7.
my $bar = "Hello!";
$foo = chop($bar); #chop() remove and return the last char of a string. So $bar == "Hello" and $foo == "!".
$bar = "Hello!\n";
$foo = chomp($bar); #chomp() remove the trailing '\n' of one or more strings and return the count of deleted chars. So $bar == "Hello!" and $foo == 1.
$foo = reverse($bar) #$foo = "!olleH".
$foo = substr($bar, 0, 4); #substr() return the substring of the given one, starting at the given index and of the given length. So $foo == "Hell".
$foo = substr($bar, 1); #If the length is omitted, all the right part of the string is returned. So $foo == "ello!".
susbtr($bar, 5) = " world!"; #susbtr() can be used as a lvalue. So $bar == "Hello world!".
index($bar, "l"); #index() return the position of the first occurrence in the given string of the given substring. Here 2.
index($bar, "l", 3); #The index where the search start can be specified. Here the result is 3.
rindex($bar, "l"); #rindex() works as index() but starting from the end of the string. Here the result is 9.

String context specificities

my $foo = 4 . "th"; #In string context numbers are converted to the string of its decimal representation. $foo == "4th".
$foo = undef; #undef will be converted to the void string "", but a warning will be printed if the 'use warnings' pragma is set.

Perl generalities (Previous chapter) < Scalars in Perl > (Next Chapter) Forthcoming

Something to say?

Leave a Reply

Your email address will not be published.