in reply to References Explanation
Comments posted to date are on target. It might help to conceptually break apart what you're doing when you mash together things like \@ or $$ or @$. Here's your example, using {} to break things up conceptually/visually:
#!/usr/bin/perl use strict; my @array = ('1','2','3','blue','red'); # Get an array reference (via "\@") for the array called "array" my $array_reference = \@{array}; &print_array($array_reference); # print 'blue' # lookup the fourth array element "${X}[3]" of the X defined by $array +_reference print "COLOR: ${$array_reference}[3]\n"; sub print_array { my $reference = shift; # access as an array the thing defined by $reference foreach (@{$reference}) { print "IN THE SUB: $_\n"; } }
The $$x and @$x are shortcuts to ${$x} and @{$x} -- in short, they tell perl how you want the reference to be interpreted -- either as a scalar or an array. The funny one might seem to be $$x[], but just as you access an element of @a with $a[], the array you could get via @{$x} you can access an element of with ${$x}[].
The tutorials suggested above are a good start for more detail
-xdg
Edited to fix display of [].
|
|---|