http://qs1969.pair.com?node_id=221564

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

Greetings fellow monks,
On pg 95 of Camel SE there is a discussion of C operators missing from Perl. A comment is made that "Perl's variable prefix characters serve as dereference operators".

What does that mean?
Is that solely in the context of something like:

${$orig_ref} += 100;

Or is a $ always dereferencing in some way I don't get?
TIA
jg
_____________________________________________________
"The man who grasps principles can successfully select his own methods.
The man who tries methods, ignoring principles, is sure to have trouble.
~ Ralph Waldo Emerson

Replies are listed 'Best First'.
Re: Dereferencing and comment from Camel
by djantzen (Priest) on Dec 21, 2002 at 04:51 UTC

    It just means that given $array_ref = [qw/bar baz/]

    $$array_ref[0] is the same as $array_ref->[0]

    Similarly, given $hash_ref = { bar => 'baz' }

    $$hash_ref{bar} is the same as $hash_ref->{bar}

    I've traditionally preferred the double-sigil style, mostly because I strongly disliked the arrow syntax when I first started coding Perl. I think though it is generally regarded as bad because it isn't quite so explicit, and I admit my own opinion on the matter is gradually shifting toward the arrow.

    Another example:

    %hash{array_ref} = []; push @{$hash{array_ref}}, 'thingy';

    Here the reference is a value in a hash that points to an array which must be dereferenced before being passed to push

      Donkey shane!
      jg
      _____________________________________________________
      "The man who grasps principles can successfully select his own methods.
      The man who tries methods, ignoring principles, is sure to have trouble.
      ~ Ralph Waldo Emerson
Re: Dereferencing and comment from Camel
by pg (Canon) on Dec 21, 2002 at 05:26 UTC
    You are 80% right, but it means more than $, you should also include @, %, &.

    That is actually sort of comment for a person who is familiar with c/c++. In c/c++, there is a single de-ref operator *. It doesn't matter what the pointer points to, you can always use * to de-ref it.

    However in Perl, we do not have such a single operator for de-ref. We have to use @, %, &. There is a big difference. In Perl, in order to de-ref a ref, you have to first know what the ref refs to. In c/c++, type doesn't matter at all, you can always cast a pointer to a certain type, which may have nothing to do with the original type, and then de-ref it, as long as don't corrupt the memory boundary. (In real life, you almost always deref a pointer to something meaningful in the context)
    In Perl, if you do:
    %a = (1,1,2,2); $a = \%a; print $$a;
    It gives you an error, and says that $a is not a SCALAR ref. You have to do:
    %a = (1,1,2,2); $a = \%a; print %{$a};
    In c/c++, * also de-ref's pointers to functions, in Perl, we have to use &:
    sub a { print "abc"; } $a = \&a; &{$a}; # call a, and prints abc
Re: Dereferencing and comment from Camel
by PodMaster (Abbot) on Dec 21, 2002 at 04:53 UTC
    Nope, I think that's about it, unless you consider slices a form of dereferencing ;)(in which case the $ sigil ain't involved)
    my %hash = ( abe => 1, lincoln => 2 ); print for @hash{ abe => lincoln }; __END__


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.