$$name - Replacing Identifiers by Scalar Variables

This section provides a tutorial example on how to use symbolic references by replacing identifiers with string scalar variables like, $$name.

As mentioned in the previous section, if a variable or function identifier is stored in a scalar variable, the scalar variable can be directly placed in the place where the identifier should be. The following tutorial program shows you some examples:

#- SoftRef1.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
   $name = 'foo';
   $$name = 10; print "$foo\n";           # the scalar of $foo
   @$name = (20); print "$foo[0]\n";      # entire array of @foo
   $$name[0] = 30; print "$foo[0]\n";     # an element of @foo
   @$name[0] = (40); print "$foo[0]\n";   # a slice of @foo
   %$name = ('k',50); print "$foo{k}\n";  # entire hash of %foo
   $$name{'k'} = 60; print "$foo{k}\n";   # an element of %foo
   @$name{'k'} = (70); print "$foo{k}\n"; # a slice of %foo
   &$name(80); sub foo {print "$_[0]\n";} # calling &foo(80)

Here is the output of the tutorial program:

10
20
30
40
50
60
70
80

Please note that references take higher precedence than subscriptions: [], lookups: {}, and function calls: (). For example, $$name[0] will be evaluated $$name fist, not $name[0] first.

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

 Using Symbolic References

$$name - Replacing Identifiers by Scalar Variables

 ${EXPR} - Replacing Identifiers by Expressions

 EXPR->* - The Dereference Operator

 $$$name - Nested Symbolic References

 Hard References - Addresses of Memory Objects

 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

 References

 PDF Printing Version