Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Dereferencing and comment from Camel

by pg (Canon)
on Dec 21, 2002 at 05:26 UTC ( #221574=note: print w/ replies, xml ) Need Help??


in reply to Dereferencing and comment from Camel

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


Comment on Re: Dereferencing and comment from Camel
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://221574]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-09-19 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (128 votes), past polls