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


Dereferencing a Reference

September 28, 1998

Once you have created a reference, you will need to get at the value that the reference holds. However, recall that references hold "pointers" to memory slots. So if you simply access the value of the reference, you probably won't get what you want.

Consider the following case:

     #!/usr/local/bin/perl
     $my_scalar = "Selena Sol";
     @my_array = ("1", "2", "3");
     $scalar_reference = \$my_scalar;
     $array_reference = \@my_array;
     print "$scalar_reference\n" .
            "$array_reference\n";

Check out the results! Notice that for $scalar_reference, Perl prints out the odd looking value "SCALAR(Ox1011e9e8)" instead of "Selena Sol".

[Example 1]

Well, Perl is doing the requested thing, of course, and printing out the value of the reference that corresponds to a location in memory where the contents of $my_scalar are actually held.

Of course, it is most likely that what you really want is the value the reference is pointing to. Well, to get this value, you must "dereference" the reference.

Dereferencing scalar, array, hash and subroutine references follow a very similar pattern as exemplified below:

     #!/usr/local/bin/perl
     $my_scalar = "Selena Sol";
     @my_array = ("1", "2", "3");
     %my_hash = {`name => `Gunther', };
     $scalar_reference = \$my_scalar;
     $array_reference = \@my_array;
     $hash_reference = \%my_hash;
     print "$$scalar_reference\n" .
           "@$array_reference[0]\n" .
	   "$hash_reference->{`name'}\n";

Notice that we use an extra $ and @ in the case of dereferencing scalars and arrays, and that we use the -> for hashes. Check out the results of this little program:

[Example 2]

Finally, a subroutine reference can be dereferenced using an "&" before the reference name such as:

     #!/usr/local/bin/perl
     sub printMessage
         {
	 my ($string) = @_;
         print "$string\n";
         }
     $subroutine_reference = \&printMessage;
     &$subroutine_reference("Hello Cyberspace");

Creating References
Introduction to Perl 5 | Table of Contents
Anonymous References



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




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