Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


The Perl You Need to Know Part 11: A Reference of References

March 13, 2000

Last month we began building complex data structures in Perl using lists, hashes, and combinations thereof. In doing so, we've flirted with the subject of references. This month our flirtation blooms into intimacy, as we cuddle up to Perl's references to help us build and manipulate complex data structures. Aside from a general understanding of Perl, as detailed in our ongoing The Perl You Need to Know series, this article is best read following last month's Part 10, Untangling Lists and Hashes.

Getting to Know You, my Reference

You are, of course, by now familiar with Perl's basic data structures, including scalar variables, lists, and hashes. Like any variables in nearly any programming language, these variables contain data, be it numerical, character strings, or whatever. A Perl reference is similar to these variables, with one crucial difference: rather than contain data itself, a reference simply points to another variable.

Consider a simple example:

$name="Moe";
$name_reference=\$name;

The normal scalar variable $name contains the string data "Moe". The reference $name_reference points to the variable $name. This notion of a "pointer" is important but sometimes obtuse to understand. It may help to consider analogies from other domains.

  • Both the Macintosh and Windows desktops use the pointer concept for what they call aliases and shortcuts, respectively. In these cases, filenames and/or icons contain no actual data, but simply lead or point to another file which does. Unix systems have symlinks which are used similarly.

  • On the Internet, one could think of a domain name as a pointer of sorts, such as estuff.com. Since machines are networked using numerical IP addresses, the domain name is a pointer to an IP address.

  • An usher at the stadium or opera house is a pointer, sometimes literally. You display your ticket, and the usher points, "Your seat is over there." In fact, the ticket is also a kind of pointer, since it is not the physical seat itself.

This is very different from, say,

$name2=$name;

Because in the above, $name2 makes a one-time copy of the value of $name. Later changes to $name don't affect $name2. But because a reference is a pointer, any change made to the value of $name will be reflected anytime you get its value via the reference $name_reference.

However you picture it, the "point" is that a Perl reference contains no data, but points at a data structure, be it a scalar, list, or hash, that does. The reference itself is always treated as a scalar in Perl, meaning that it is always preceded by a dollar sign ($), regardless of what type of variable it points to. Thus:

@cats=("Harvey","Smitty");
$catref=\@cats;
%catbreeds=("tabby"=>"Harvey","calico"=>"Smitty");
$catbreedref=\%catbreeds;

In both cases above, the references are specified using the scalar notation, despite the fact that they point to a list and a hash respectively.

Constructing a reference itself is rather easy., as you can see. The real issues lie beyond creating the reference ... such as what to use them for, and just how to follow these pointers to the pot of gold.

Contents:

Dereference, my dear
Shall we dance, sweet reference?
Take my Parameters, Please
Come Sail Away

The Perl You Need to Know
Dereference, my dear


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers