# Scalar Value Constructions

## Perl Data Types – Part 3

### Perl Course

Foreword: In this part of the series, I talk about scalar value constructions.

By: Chrysanthus Date Published: 22 May 2015

### Introduction

This is part 3 of my series, Perl Data Types. In this part of the series, I talk about scalar value constructions. All data in Perl is a scalar, an array of scalars, or a hash of scalars. A scalar contains one single value in any of three different flavors: a number, a string, or a reference. A scalar variable is preceded by \$. You should have read the previous parts of the series before reaching here, as this is a continuation.

Number
An integer is a whole number. A number can be an integer, as the following statement shows:

\$num = 54;

A number with a decimal point is a floating-point number. A number in Perl can be a float as the following statement shows:

\$num = 23.48;

In Perl, if a number is long, you do not use commas to group the digits in threes; you use but the underscore, as in the following statement:

my \$num = 4_294_967_296;

This is the number, 4294967296, written on paper as 4,294,967,296, but written in Perl as 4_294_967_296 or 4294967296 (without any separation).

You can write a number in Perl in what is known as Perl’s Exponential Form. The number 256.39 can be written in Perl’s exponential form as,

2.5639E+2

To code a number this way, you begin with a digit. This is followed by a decimal point. After that you have the rest of the digits without any other decimal point. Then you have the letter, E. This is followed by + or - .You type + if the decimal point has to be moved to the right to reach its original position. You type – if the decimal point has to be moved to the left to reach its original position. Then you type a number for the number of digits the decimal point has to be carried over to reach its original position.
String
In Perl, a normal string literal is text in double quotes or single quotes. For a double quoted string, a variable in the string is substituted by its value, and an escape sequence in the string takes its effect. For a single quoted string, a variable is not substituted by its value and an escape sequence does not take effect, except for \' and \\ .

So for the code segment,

my \$quantity = 50;
my \$str = "I have \$quantity of them.\nYou have nothing.";
print \$str;

The string would be printed as:

I have 50 of them.
You have nothing.

At the output, \$quantity has been replaced by 50 and the \n has had its effect creating a new line. Neither \$quantity nor \n could have caused any change if the string was within single quotes.

Try the following code where the string is delimited by single quotes:

use strict;

my \$quantity = 50;
my \$str = 'I have \$quantity of them.\nThe \\ and \' take effect';
print \$str;

The output is:

I have \$quantity of them.\nThe \ and ' take effect

Since the string is within single quotes, \$quantity has not been expanded (replaced by its value); \n has not had its effect. The single quote exceptions of \\ and \' have had their effects: \\ is the escape sequence for \ and \ alone has been displayed; \' is the escape sequence for ' and only ' has been displayed.
Special Literals
Special literals are: __FILE__, __LINE__, __PACKAGE__, __SUB__, __END__ and __DATA__ . Here I will only talk about __FILE__ and __LINE__ . I will talk about the rest later as we go along in the course. Each of these literals has double underscores, __.

__FILE__ represents the current filename. I tried

print __FILE__ ;

in my computer and I had

C:\sample.pl

where C:\ is the directory path and sample.pl is the name of the file.

__LINE__ represents the current line number in the file. I tried

print __LINE__ ;

in my computer and I had

10

because the print statement was typed at line 10 in the file, counting from the top. Note: line counting begins from 1 and not zero.

Reference
A reference is the equivalent of the starting address of an entity in memory. Try the following code:

use strict;

my \$myVar = "We are the world.";
my \$item  = \\$myVar;
print \$item, "\n";

In memory, you have the entity, "We are the world.", which is identified by the variable, \$myVar. To obtain a reference to this scalar entity, precede the \$myVar variable with \ . The return value of that is held by the scalar variable, \$item in the code. This is not holding a normal value (string or number); it is holding a reference, which is a kind of scalar. In my computer the printing of \$item gives,

SCALAR(0xe2fb8c)

which is of the starting address of the string entity.

Bareword
A bareword can be considered as a stray word in code, not preceded by \$ or @ or % or &. Its presence is an error.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Miscellaneous Features in Perl
Perl Two-Dimensional Structures