Broad Network


Reference to a Scalar in Perl

Perl References Optimized – Part 1

Perl Course

Foreword: In this part of the series, I specifically talk about Perl reference to a scalar.

By: Chrysanthus Date Published: 10 Jul 2015

Introduction

This is part 1 of my series, Perl References Optimized. A reference in Perl is a short piece of code (address) that refers to a region in the computer’s memory. How you use that reference is what we learn in all of this series. Knowing what a reference is, is not a big deal. You already know what it is from the statement above. How you use it is what we learn in the whole of this series. In this part of the series, I specifically talk about Perl reference to a scalar.

The code samples of this series are of ActivePerl. If you are using traditional Perl, then begin each code sample with something like, #!/usr/bin/perl .

Pre-Knowledge
This series is part of the course, Optimum Perl Coding. At the bottom of this page you will find links to the different series you should have covered before coming here. The titles of the series you should have learned are: Perl Basics, Perl Data Types and Perl Syntax, which form the Perl Essentials.

Memory Regions
Memory is a series of cells. Each cell in memory can store one character. A consecutive number of cells in memory storing particular information, is a region. Consider the following two statements:

$var1 = "I am a big man.";
$var2 = "I am a big man.";

In the above statements, you have the same text (string) assigned to two different variables. The contents of the two variables are the same, but unfortunately the two contents occupy two different regions in memory. If you really want things that way, well, there is no problem. However, you usually will not want the same content in more than one region in memory. Referencing allows you to have one content in memory and let more than one variable refer to the value (content). In this part of the series we talk about a scalar reference.

Creating a Reference to a Scalar
A scalar is a number or a string. Assume that a variable is holding a scalar. You create a reference to the region of the scalar by preceding the variable with a back slash (). The following two statements illustrate this:

$var1 = "I am a big man.";
$sref = \$var1;

For the above two statements, we have one region in memory that has the string value, "I am a big man.". This value is held by the scalar variable, $var1, as seen from the first statement. The second statement also has a scalar variable as its left operand. The right operand of the second statement is the scalar variable of the first statement preceded by the backslash. Preceding this variable with the backslash produces a reference (address), which is now assigned to the ordinary scalar variable, $sref. So $sref holds a reference to the value (content) and not the value.

Obtaining the Scalar Value from a Reference
In the above two statements, $sref holds a reference. How do you get (retrieve) the value, "I am a big man.", referred to by the reference from $sref? The following expression will produce the value:

    ${$sref}

The reference is to a scalar, so you begin with the scalar sign, $. Then you have the pair of braces. In between the braces you have the scalar variable that holds the reference. Remember, a reference is a memory address. The above can be abbreviated as:

    $$sref

Read and try the following code, which illustrates the use of Reference to a Scalar Value:

use strict;

my $var1 = "I am a big man.";
my $sref = \$var1;
print ${$sref};

Replace the statement, “print ${$sref};” with “print $$sref;” above and retry the code again to have the same result.

Note: When dealing with a reference, you are more interested in the region occupied by a value in memory than what identifies (the variable that holds) the value.

That is it for Perl Reference to a Scalar. We stop here and continue in the next part of the series.

Chrys

Related Links

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
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message