On Tue, 25 Sep 2001, David Simcik wrote:

>       This is a newbie question if there ever was one, but what is the difference
> between a (Perl) reference and a (C/C++ style) pointer??? The only thing
> that seems immediately obvious is that C/C++ pointers are strongly
> typed...???

A pointer in C & C++ gives you a live chunk of memory from the heap -- and
it is the programmer's responsibility to reference and dereference it
properly, especially in terms of allocation and deallocation.  And not all
pointers need to be typed -- you can use void pointers (but still have to
ultimately typecast them to do useful stuff with them).

In Perl, references are more like the file references we use in a Unix
filesystem -- a symbolic reference refers to the *name* of a file
(variable), whereas a hard reference refers to the actual contents of a
file (variable).  Symbolic references are frowned upon in Perl mostly
these days.  In Perl also, you don't have to worry about allocating and
deallocating memory for references -- they come and go as needed (yes
there is some overhead for this).

As an example:

symbolic reference

my $var = 12;

my $varref = "var"; #point to the variable named $var

a hard reference (or just reference, since usualyl when people say
reference they mean hard reference):

my $var = 12;
my $varref = \$var; #point to the value in $var

References in Perl are more like references in C++ and less like pointers
in C.

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
He who laughs, lasts.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to