in reply to References Explanation

A reference is like a pointer from C and C++, except that it will always point at something (a reference can't be null or pointing to invalid memory).

So $array_reference is a variable which contains a pointer to the same memory as @array contains. That means that @array and @$array_reference (note the @ symbol there) are the same thing, but $array_reference (no @ symbol) is a different thing.

When the reference is passed to the sub, a copy of it is made. This copy also points to the same memory as @array, so @$reference is still the same thing as @array. If you tried to pass @array to the sub directly, the sub would have a copy of it and it would only be able to change the copy, but not be able to change the real @array.

You can read the tutorial for more information.

Replies are listed 'Best First'.
Re: Re: References Explanation
by Mr. Muskrat (Canon) on Jun 26, 2003 at 14:46 UTC

    A reference can't be null but it can reference empty strings, null characters and undefined values.

    #!/usr/bin/perl -w use strict; foreach my $ref (\'', \"\0", \undef) { print $ref, " --> ", ${$ref}, "\n"; }
    Note the warning about using an uninitialized value.