Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Returning two hashes from a sub?

by BrowserUk (Pope)
on Mar 01, 2004 at 22:27 UTC ( #333077=note: print w/replies, xml ) Need Help??


in reply to Returning two hashes from a sub?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://333077]
help
Chatterbox?
[marto]: this sounds like a sub section for Sane Dots
[Corion]: marto: You mean "Sane Dots Dramatic Reading of ITIL" ? ;)
[marto]: Sane Dots, chapter 13: ITIL never work :P
[marto]: sorry, bad pun of it'll :P
[Corion]: :-D
[Corion]: I think that ITIL is mostly to prevent people from doing harm to a system that basically runs unattended :)
[Corion]: Maybe you could reduce ITIL to a computer network, a man and a dog. The network runs the computations. The man is there to feed the dog. The dog is there to prevent the man from accessing the computers.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2017-01-24 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (203 votes). Check out past polls.