Broad Network


Perl Reference to a Scalar

Perl References - Part 1

Forward: 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.

By: Chrysanthus Date Published: 12 Aug 2012

Introduction

This is part 1 of my series, Perl References. 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.

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 .

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Prerequisite
You need to have basic knowledge in Perl before reading this series. You also need basic knowledge in HTML or XHTML. If you do not have any such knowledge, then read the series whose first parts are titled,

Getting Started with HTML
Getting Started with ActivePerl

To arrive at a series, type this title and my name, Chrys, in the Search Box of this page and click Search.

I use the ActivePerl Interpreter from ActiveState and the Abyss Web Server X1 personal web server, for this series.

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;

print "Content-Type: text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>Code Sample</title>\n";
print "</head>\n";
print "<body>\n";

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

print "</body>\n";
print "</html>\n";

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 Reference
Object Oriented Programming in Perl
Date and Time in Perl
Regular Expressions in Perl
Perl Course
Web Development Course
Major in Website Design
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message