Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Dereferencing and comment from Camel

by pg (Canon)
on Dec 21, 2002 at 05:26 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-29 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found