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

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

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

Here is the output of the tutorial program:


Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

Hard References - Addresses of Memory Objects

 \* - 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

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing


 PDF Printing Version