Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://221574]
help
Chatterbox?
[Tux]: (ambiguous) My pm-cb-g still works ok
[choroba]: I'll add an option to specify which url to connect to
choroba talking from pm-cb-g at the moment

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2017-10-24 11:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (288 votes). Check out past polls.

    Notices?