zetetes has asked for the wisdom of the Perl Monks concerning the following question:

so i wanted to do some work with hash-references. nothing easier than that:
my %list = { 'lang' => 'de' }; my $listref = \%list; print $listref->{'lang'};
should print something like
HASH(0x1fc0e)

that's what i thought too. but then, i got an error saying
Not a HASH reference at ...

debugging tells me, that $listref contains REF(0x80bf20). after doing some research, i got to know that REF(...) is a 'destination of a reference', not the reference itself.
so: thank you guys for any help.
(i do use -w and use strict. )

Replies are listed 'Best First'.
Re: REF instead of HASH-ref
by VSarkiss (Monsignor) on Sep 14, 2004 at 22:05 UTC

    my %list = { 'lang' => 'de' };
    You want:
    my %list = ( 'lang' => 'de' ); my $listref = \%list;
    (Note the parens).

    Or you can do my $listref = { 'lang' => 'de' };(Note the curly braces).

    Once you do that, print $listref->{'lang'} should print de, not any kind of stringified hash.

    Check out perlref for more detail. Basically, the parenthese produce a list (which you can assign to a hash and then take a reference), and the curly braces produce an anonymous hash, whose reference you can store in a scalar.

    Update
    Added a little more detail....

      if i do
      my %list = ( 'lang' => 'de' ); my $listref = \%list; print ref($listref);
      i get REF. if i do
      my $listref = { 'lang' => 'de' }; print ref($listref);
      i get HASH.

      i use this with CGI. so i initialize the hash as follow:
      use vars qw($query @parname %list $n ); $query = new CGI; @parname = $query->param; foreach $n (@parname) { $list{$n} = $query->param($n); }
      i then just pass \%list along to a function which takes the refenrence and uses the hash again. i used exactly this code (copy/paste) in another app, there it worked fine. the environment is the same, too.

      kind of puzzled. update: if i update the initialization to:
      $query = new CGI; @parname = $query->param; foreach $n (@parname) { $hashref->{$n} = $query->param($n); }
      everything works fine. hm...