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
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
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.
|
| [reply] [d/l] |