Assign reference to hash returned from a function

H

Hobo Salesman

I'm using Config::General, the getall method returns a hash. I want to
store this hash in a reference, and I'm curious about what exactly is
going on and couldn't find clarification reading docs.

#This assigns '6/8'... why do I get a string like that when assigning a
hash to a scalar?
$conf = $confGen->getall;

#This seems to make a code reference that contains a hash...?
$conf = \($confGen->getall);

#This assigns a ref to an empty hash
$conf = \%{$confGen->getall};

#This is a little ugly
my %conf = $confGen->getall;
$conf = \%conf;
undef %conf;

Please shed some light on this for me!

HS
 
X

xhoster

Hobo Salesman said:
I'm using Config::General, the getall method returns a hash.

Apparently it returns whatever a hash evaluates to in the context
in which getall is called. In neither case is that a hash, it is either
a string, or a flattened list of alternating key-value pairs.
I want to
store this hash in a reference, and I'm curious about what exactly is
going on and couldn't find clarification reading docs.

#This assigns '6/8'... why do I get a string like that when assigning a
hash to a scalar?
$conf = $confGen->getall;

from perldata:

If you evaluate a hash in scalar context, it returns false
if the hash is empty. If there are any key/value pairs,
it returns true; more precisely, the value returned is a
string consisting of the number of used buckets and the
number of allocated buckets, separated by a slash.

So you now know that the hash inside the function had 8 buckets, of
which 6 were occupied.

You need to use the construct which one uses to make a hashref out of
a list of key value pairs. That is the curlies:

$conf = {$confGen->getall}
#This seems to make a code reference that contains a hash...?
$conf = \($confGen->getall);

#This assigns a ref to an empty hash
$conf = \%{$confGen->getall};

You probably aren't using strict. Shame on you.

Xho
 
T

Tad McClellan

Hobo Salesman said:
I'm using Config::General, the getall method returns a hash.


That is not possible in Perl.

The getall method returns a *list* (as do all Perl subroutines).

I want to
store this hash in a reference,


You don't have access to the hash, only to a list (derived from
the hash).

You can _copy_ the list into a hash reference by using the
anonymous hash constructor (curly braces):

$conf = { $confGen->getall };

and I'm curious about what exactly is
going on and couldn't find clarification reading docs.

#This assigns '6/8'... why do I get a string like that when assigning a
hash to a scalar?
$conf = $confGen->getall;


Because that is what a hash in scalar context is supposed to do.

A hash used in a scalar context returns info that is not
generally useful to a Perl programmer (you), it is only useful to a
perl programmer (who are actually C programmers, the p5p).

It returns info about the underlying hashing that is builtin to perl.

From the "Scalar values" section in perldata.pod:

"the number of used buckets and the number of allocated buckets,
separated by a slash."
 
H

Hobo Salesman

Gunnar said:
Because that's what you get when evaluating a hash in scalar context.

Do the numbers mean something or have some purpose? First one seems
like it could be the number of elements?
Try this:

my $conf = { $confGen->getall };

Thanks.
 
G

Gunnar Hjalmarsson

Tad said:
That is not possible in Perl.

The getall method returns a *list* (as do all Perl subroutines).

C:\home>type test.pl
sub rethash { my %hash = (one => 1, two => 2); %hash }
print scalar rethash(), "\n";

C:\home>perl test.pl
2/8

C:\home>
 
J

John W. Krahn

Gunnar said:
C:\home>type test.pl
sub rethash { my %hash = (one => 1, two => 2); %hash }
print scalar rethash(), "\n";

C:\home>perl test.pl
2/8

C:\home>

That is still returning a list, a list with one element, but a list nonetheless.



John
 
H

Hobo Salesman

You probably aren't using strict. Shame on you.

Actually I am using strict, and going to lengths to use proper OOP
techniques too, but I think maybe while testing stuff out interactively
in teh debugger 'use strict' wasn't in place.
 
H

Hobo Salesman

Tad said:
You don't have access to the hash, only to a list (derived from
the hash).

Sorry, I meant that it returns what's meant to be interpreted as a
hash. I'll try to be a little clearer in any future posts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top