Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: hash from sub directly into foreach loop

by gellyfish (Monsignor)
on Jun 30, 2006 at 10:58 UTC ( [id://558551]=note: print w/replies, xml ) Need Help??


in reply to Re^2: hash from sub directly into foreach loop
in thread hash from sub directly into foreach loop

You can't "return a hash" from a subroutine, you can only return a list, the effect of return %hash is to flatten the hash into a list of key => value pairs. Thus the effect of the the three following subroutines is similar (the order of the hash keys in the first aside:)

sub foo { my %foo = ( Foo => '1', Bar => '2'); return %foo; } + sub bar { my @bar = qw(Foo 1 Bar 2); return @bar; } + sub baz { return 'Foo','1','Bar','2'; } + print foo(),"\n"; print bar(),"\n"; print baz(),"\n";

This is why people are telling you to return a hash reference as this is the only way to retain the, er, hashiness after the appropriate dereferencing.

/J\

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://558551]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found