tlk00 has asked for the wisdom of the Perl Monks concerning the following question:
I believe this is called the typeglob approach. The original example code is below.#!/usr/bin/perl $fruit{'Apple'} = "Red"; $fruit{'Orange'} = "Orange"; $fruit{'Grape'} = "Purple"; $fruit{'Lemon'} = "Yellow"; $fruit{'Lime'} = "Green"; $myHashref=\%fruit; print "Hash reference to send = $myHashref\n"; print "Print local hash\n\n"; foreach $key (sort keys %fruit) { print $key, '=', $fruit{$key}, "\n"; } print "\nLeaving Main\n\n"; &mysubrtn($myHashref); sub mysubrtn() { local *myref = shift ; print "In my Subrtn\n"; print "Print hash via reference\n\n"; foreach $key (sort keys %myref ) { print $key, '=', $myref{$key}, "\n"; } }
[root@dubya perl]# cat fruit2.pl #!/usr/bin/perl $fruit{'Apple'} = "Red"; $fruit{'Orange'} = "Orange"; $fruit{'Grape'} = "Purple"; $fruit{'Lemon'} = "Yellow"; $fruit{'Lime'} = "Green"; $myHashref=\%fruit; print "Hash reference to send = $myHashref\n"; print "Print local hash\n\n"; foreach $key (sort keys %fruit) { print $key, '=', $fruit{$key}, "\n"; } print "\nLeaving Main\n\n"; &mysubrtn($myHashref); sub mysubrtn() { $myref = shift ; print "In my Subrtn\n"; print "Print hash via reference\n\n"; foreach $key (sort keys %$myref ) { print $key, '=', ${ $myref }{$key}, "\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing hash reference to a subroutine
by hbm (Hermit) on Feb 19, 2009 at 17:03 UTC | |
by tlk00 (Initiate) on Feb 20, 2009 at 14:54 UTC | |
by hbm (Hermit) on Feb 20, 2009 at 15:48 UTC | |
|
Re: Passing hash reference to a subroutine
by jethro (Monsignor) on Feb 19, 2009 at 17:02 UTC |