coldmiser has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Small troubles with references
by wfsp (Abbot) on Feb 09, 2006 at 20:12 UTC | |
by injunjoel (Priest) on Feb 09, 2006 at 21:00 UTC | |
|
Re: Small troubles with references
by MCS (Monk) on Feb 09, 2006 at 20:15 UTC | |
by duff (Parson) on Feb 09, 2006 at 20:25 UTC | |
by demerphq (Chancellor) on Feb 09, 2006 at 20:33 UTC | |
by ptum (Priest) on Feb 09, 2006 at 23:08 UTC | |
by demerphq (Chancellor) on Feb 09, 2006 at 23:17 UTC | |
by runrig (Abbot) on Feb 09, 2006 at 23:15 UTC | |
|
Re: Small troubles with references
by duff (Parson) on Feb 09, 2006 at 19:44 UTC | |
by coldmiser (Hermit) on Feb 09, 2006 at 19:50 UTC | |
by swkronenfeld (Hermit) on Feb 09, 2006 at 20:29 UTC | |
|
Re: Small troubles with references
by Cody Pendant (Prior) on Feb 09, 2006 at 23:49 UTC |