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

I have been hacking away at PERL for a number of years now (I don't want to infere that I actually know what I am doing), and I am confused about hard references and symbolic references. Why would you ever need to use a hard reference? I have read the explanation in the camel book a couple of times, but I am just not getting it. Thanks for all your help, I hope I am not being too annoying.

Edit kudra, 2001-09-21 Changed title

Replies are listed 'Best First'.
Re: I hope this question isn't too basic...
by blakem (Monsignor) on Sep 20, 2001 at 22:30 UTC
    Dominus has written a good reference tutorial that you might want to look at.

    In general, almost all uses of symbolic references would be better implemented as keys in a regular hash. So your question should really be "Why would you ever need to use a *symbolic* referece?"....

    -Blake

      Not only Dominus has written a good reference tutorial, but he also has written some stuff explaining Why it is stupid to use a variable as a variable name. In other words, think before you use symbolic references. Up to now, I have never encountered a situation where symbolic references cannot be replaced by a hash.
      forgive me if i don't have a clue to what i am talking about, but isn't $foo{boo} a symbolic refernce, while $foo->{boo} a hard reference?
        Nope, take a look at the following code, especially the third stanza:
        #!/usr/bin/perl -wT use strict; # hash key lookup using the actual hash (no reference) my %foo = (bar => 'data'); my $val = $foo{bar}; # fetch the scalar key (hence the '$' infron +t of foo) from the hash print "Val => $val\n"; # hash key lookup using a referent to the hash (hard reference) my %hash = (bar => 'data2'); my $foo = \%hash; my $val2 = $foo->{bar}; # can also be written '$$foo{bar}'; print "Val2 => $val2\n"; # using symbolic (soft) references use vars qw($foo $bar); $bar = 'data3'; $foo = 'bar'; my $val3; { no strict 'refs'; # 'use strict' won't allow this $val3 = $$foo; # find the data by treating the value of $foo +as a var name } print "Val3 => $val3\n"; =output Val => data Val2 => data2 Val3 => data3

        -Blake

        Nope. A symbolic reference is something like:
        $bar = 3; $foo = 'bar'; $baz = $$foo; # This now sets $baz = $bar, or 3.
        Any other reference is a hard reference. $foo{boo} is a hash-access, not a reference at all.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.