Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

There is a technique mentioned in perlsub for dealing with this. Although it may be seen as old-fashioned, it still works fine, and allows you to deal with "real hash" syntax inside and outside the sub without resorting to global vars or en masse copying of data.

#! perl -sw use strict; sub getRefs{ my( %h1, %h2 ); @h1{ qw[the quick brown fox] } = (1..4); @h2{ 'a'..'z' } = 1 .. 26; return \%h1, \%h2; } { our( %glob1, %glob2 ); local( *glob1, *glob2 ) = getRefs(); print "$_ $glob1{ $_ }\n" for keys %glob1; print "$_ $glob2{ $_ }\n" for keys %glob2; } __DATA__ the 1 fox 4 brown 3 quick 2 w 23 r 18 a 1 x 24 d 4 j 10 y 25 u 21 k 11 h 8 g 7 f 6 t 20 i 9 e 5 n 14 v 22 m 13 s 19 l 12 c 3 p 16 q 17 b 2 z 26 o 15

What you do is assign the returned hashref to (localised) glob. You can then access the has via that glob name using standard hash variable syntax instead hashref syntax. It does simplify the syntax without the need to copy and avoiding the risks of global vars.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Re: Returning two hashes from a sub? by BrowserUk
in thread Returning two hashes from a sub? by matija

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (8)
    As of 2019-10-18 08:38 GMT
    Find Nodes?
      Voting Booth?