Reputation: 968
what is the difference between ->
and $
in Perl
where ->
is infix dereference operator.
$
is also dereference operator.
what exactly meaning of this operator?
Upvotes: 0
Views: 259
Reputation: 57600
Yes, both $
and ->
are dereference operators, even though they are quite different.
Perl allows you to use references to other data. These are roughly similar to pointers in some languages. To get the original data structure, we have to dereference them. This generally involves curly braces (which can be omitted in trivial cases) and the sigil of the type we are dereferencing to. (Most values can only be dereferenced to one type, else an error is thrown).
${ $scalar_ref };
@{ $array_ref };
%{ $hash_ref };
*{ $glob_ref };
&{ $code_ref }(@args);
If the $type_ref
is just a variable, the curlies can be omitted, but they are practical when we have more complex expressions.
Now the problem is to access fields in hashes or arrays, without assigning to an intermediate hash. This is where the ->
operator is used:
# instead of
my %hash = %{ $hashref };
my $field = $hash{field};
# we can do this and avoid unneccessary copying :)
my $field = ${$hashref}{field}; # curlies around $hashref optional
my $field = $hashref->{field};
Similar for arrays, coderefs, and method calls on objects:
$array_ref->[$index]; $$array_ref[$index];
$code_ref->(@args); &$coderef(@args);
$object->method(@args);
Actually, what the ->
operator does in the context of method calls (or what looks like them) is a bit more complex, but that doesn't seem the issue of your question.
For arrayrefs and hashrefs, you can just imagine that the $$ref[$i]
-like dereference simply replaces $ref
with the name of the array, which it does in a symbolic sense: $array[$i]
. So the first $
sigil is that of the array element, and the second $
that of the scalar holding the reference.
Upvotes: 4
Reputation: 20280
I cannot recommend strongly enough the core documentation references tutorial, perldoc perlreftut, written by the well-respected perler Mark Jason Dominus. It is concise and teaches a few simple rules for creating and using references.
I'm not saying RTFM, I'm saying, there is a great doc on this point, seek it out, it will help you!
Upvotes: 7
Reputation: 98388
They can be different ways of doing the same thing.
See http://perlmonks.org/?node=References+quick+reference; rule 3 there shows how some $
expressions can be changed to use ->
instead.
Upvotes: 0
Reputation: 59553
My perl is a little rusty but $
always means the following is the name of a scalar. The arrow operator (->
) is a very different beast. It means dereference the name and then apply the following operators. The following is an example from perlref(1):
Because of being able to omit the curlies for the simple case of
$$x
, people often make the mistake of viewing the dereferencing symbols as proper operators, and wonder about their precedence. If they were, though, you could use parentheses instead of braces. That's not the case. Consider the difference below; case 0 is a short-hand version of case 1, not case 2:$$hashref{"KEY"} = "VALUE"; # CASE 0 ${$hashref}{"KEY"} = "VALUE"; # CASE 1 ${$hashref{"KEY"}} = "VALUE"; # CASE 2 ${$hashref->{"KEY"}} = "VALUE"; # CASE 3
Case 2 is also deceptive in that you're accessing a variable called
%hashref
, not dereferencing through$hashref
to the hash it's presumably referencing. That would be case 3.
Upvotes: 0