Is there anything wrong with passing a hash reference like this:
#!/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"; } }
I believe this is called the typeglob approach. The original example code is below.
This doesn't seem to be as straight forward to me. Is this just my lack of understanding? Opinions?
[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"; } }

In reply to Passing hash reference to a subroutine by tlk00

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.