MarkovChain has asked for the wisdom of the Perl Monks concerning the following question:
Now in the subroutine fillerup, I expect the auto-vivification of a new hash. However, the output is as follows:1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 7 sub fillerup ($); 8 9 my %fillme = (gas_pr => 3.59, gas_capacity => 14); 10 11 12 local $\ = "\n"; 13 14 print '-'x80; 15 foreach my $key (keys %fillme ) { 16 print $key . '=' . $fillme{$key}; 17 } 18 19 fillerup ( \%fillme ); 20 21 print '-'x80; 22 23 foreach my $key (keys %fillme ) { 24 print $key . '=' . $fillme{$key}; 25 } 26 27 sub fillerup ($) { 28 no strict; 29 local *hashref = shift; 30 # my $hashref = shift; 31 32 print "Prior to inserting a record in fillerup..."; 33 print '*'x80; 34 35 foreach my $key (keys %$hashref ) { 36 print $key . '=' . $hashref->{$key}; 37 } 38 39 $hashref->{filled_up} = 12.3; 40 41 print '*'x80; 42 43 foreach my $key (keys %$hashref ) { 44 print $key . '=' . $hashref->{$key}; 45 } 46 47 print '*'x80; 48 49 return; 50 }
* Note that the output has been reformatted to promote visibility. (1) Not sure why the locally created glob does not get the reference to the fillme hash. It does get a reference but one to an anonymous hash. If I comment out line 29 and uncomment line 30, I get the following output:------------------------------------------------------------- gas_capacity=14 gas_pr=3.59 Prior to inserting a record in fillerup... ********************************************************************** +******** ********************************************************************** +******** filled_up=12.3 ********************************************************************** +******** ------------------------------------------------------------- gas_capacity=14 gas_pr=3.59
* Note that the output has been reformatted to promote visibility. (2) Also, the program fails to run when the local qualifier on line 29 is changed to our. I have read through the tutorial on how local scoping works and I thought I was clear but apparently not. I would appreciate your time and help if you could aid me in my understanding of the interaction of scoping and globs. Thanks, - Markov------------------------------------------------------------- gas_capacity=14 gas_pr=3.59 Prior to inserting a record in fillerup... ********************************************************************** +******** gas_capacity=14 gas_pr=3.59 ********************************************************************** +******** gas_capacity=14 filled_up=12.3 gas_pr=3.59 ********************************************************************** +******** ------------------------------------------------------------- gas_capacity=14 filled_up=12.3 gas_pr=3.59
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Variable scoping and globs and reference passing to subroutines!!!
by ig (Vicar) on Dec 19, 2008 at 02:20 UTC | |
|
Re: Variable scoping and globs and reference passing to subroutines!!!
by tilly (Archbishop) on Dec 19, 2008 at 06:33 UTC | |
by MarkovChain (Sexton) on Dec 19, 2008 at 14:37 UTC | |
by tilly (Archbishop) on Dec 19, 2008 at 16:02 UTC |