Assign reference to hash returned from a function

Discussion in 'Perl Misc' started by Hobo Salesman, Jun 16, 2006.

  1. 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
    Hobo Salesman, Jun 16, 2006
    #1
    1. Advertising

  2. Hobo Salesman

    Guest

    "Hobo Salesman" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jun 17, 2006
    #2
    1. Advertising

  3. Hobo Salesman <> wrote:
    > 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."


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 17, 2006
    #3
  4. Gunnar Hjalmarsson wrote:
    > 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.
    Hobo Salesman, Jun 17, 2006
    #4
  5. Tad McClellan wrote:
    > Hobo Salesman <> wrote:
    >>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).


    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>

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 17, 2006
    #5
  6. Gunnar Hjalmarsson wrote:
    > Tad McClellan wrote:
    >> Hobo Salesman <> wrote:
    >>> 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).

    >
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Jun 17, 2006
    #6
  7. wrote:
    > 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.
    Hobo Salesman, Jun 17, 2006
    #7
  8. Tad McClellan wrote:
    > 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.
    Hobo Salesman, Jun 17, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    484
  2. rp
    Replies:
    1
    Views:
    498
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    598
    David A. Black
    Jul 2, 2008
  4. Catsquotl
    Replies:
    4
    Views:
    107
    Brian Candler
    Jun 3, 2009
  5. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    108
    Steve
    Sep 28, 2003
Loading...

Share This Page