Perl Tutorials - Herong's Tutorial Examples - Version 5.32, by Dr. Herong Yang
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
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
►Hard References - Addresses of Memory Objects
$$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 Directories and Read File Names
File System Functions and Operations
Converting Perl Script to Executable Binary
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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