Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

EXPR->* - The Dereference Operator

This section provides a tutorial example on how to use hard references with the dereference operator like EXPR->[*], EXPR->{*}, or EXPR->(*) for array elements, hash elements and function calls.

As mentioned in the previous section, if a hard reference is used with a subscription of [*], {*}, or (*), the dereference operator, -> can be used between the reference expression and the subscription. Note that subscription [*] is used for array elements, {*} is used for hash elements, (*) is used for function calls.

The following tutorial program shows you some examples. Note that you can not use -> to access array slices.

#- HardRef3.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
   $foo = 0;
   @foo = (0);
   %foo = (k,0);
   $refs = \$foo;
   $refa = \@foo;
   $refh = \%foo;
   $reff = \&foo;
   @refl = (\$foo, \@foo, $refh, $reff);
   
   $refa->[0] = 30; print "$foo[0]\n";   
   $refa->[0,1] = (40,41); print "$foo[0]\n";     # not working
   $refh->{'k'} = 60; print "$foo{k}\n";  
   $refh->{'k','l'} = (70,71); print "$foo{k}\n"; # not working
   $reff->(80);                           

   (\@foo)->[0] = 130; print "$foo[0]\n";         # \@foo->[0] is bad
   ('bla',\%foo)->{'k'} = 160; print "$foo{k}\n";  
   $refl[3]->(180);                           

sub foo {print "$_[0]\n";}

Here is the output of the tutorial program:

30
30
60
60
80
130
160
180

Sections in This Chapter

\* - Creating Hard References

Using Hard References

$$name - Replacing Identifiers by Scalar Variables

${EXPR} - Replacing Identifiers by Expressions

EXPR->* - The Dereference Operator

$$$name - Nested Hard References

\$b-\$a - Using Hard References in Other Operations

Dr. Herong Yang, updated in 2008
EXPR->* - The Dereference Operator