I'm trying to get a grasp on references and I was under the impression that you could not pass hashes & arrays to a subroutine. I thought you needed to pass references to a subroutine and then dereference it once you were inside the sub.
To test this, I created this piece of code and my 'clear as mud' understanding of references became much more difficult to see through when my @array & %hash actually printed their values.
Can someone explain to me how I managed to do something I thought was not possible?
Thanks.#! perl -w use strict; my @array = ("0","1","2","3"); my %hash = ("zero" => "0", "one" => "1", "two" => "2", "three" => "3") +; my $aref = \@array; my $href = \%hash; &test($aref, $href); sub test { my $one = shift; my $two = shift; print join (' ', @$one),"\n"; print join (' ', %$two),"\n"; print join (' ', @$aref),"\n"; print join (' ', %$href),"\n"; print join (' ', @array),"\n"; print join (' ', %hash),"\n"; sleep 1; }
In reply to Small troubles with references by coldmiser
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |