arvid has asked for the wisdom of the Perl Monks concerning the following question:
Does anyone know why this code:
use strict; use Data::Dumper; sub sub_b () { my %hash1b = (); $hash1b{"key1"} = "1"; print "calling sub_c from sub_b\n"; sub_c (%hash1b); } sub sub_c (\%) { my ($hash1c) = @_; print "sub_c arguments:" . Dumper (@_); } my %hash1a = (); $hash1a{"key1"} = "1"; print "calling sub_b\n"; sub_b (); print "\n"; print "calling sub_c directly\n"; sub_c (%hash1a);
returns:
calling sub_b calling sub_c from sub_b sub_c arguments:$VAR1 = 'key1'; $VAR2 = 1; calling sub_c directly sub_c arguments:$VAR1 = { 'key1' => 1 };
?
There appears to be a difference in the way sub_c interprets the parameter, depending of it is called directly or from within another sub. Could someone perhaps explain what is going on ?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing by reference from within a sub
by RichardK (Parson) on Jul 15, 2015 at 12:16 UTC | |
by arvid (Initiate) on Jul 15, 2015 at 12:25 UTC | |
|
Re: Passing by reference from within a sub
by Anonymous Monk on Jul 15, 2015 at 12:13 UTC | |
by arvid (Initiate) on Jul 15, 2015 at 12:27 UTC | |
|
Re: Passing by reference from within a sub
by AnomalousMonk (Archbishop) on Jul 15, 2015 at 14:24 UTC | |
|
Re: Passing by reference from within a sub
by 1nickt (Canon) on Jul 15, 2015 at 12:27 UTC | |
by arvid (Initiate) on Jul 15, 2015 at 12:35 UTC | |
|
Re: Passing by reference from within a sub
by arvid (Initiate) on Jul 15, 2015 at 12:37 UTC |