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

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

This section provides a tutorial example on using hard references as numeric, string or Boolean values in other operations.

A hard reference can also be used as scalar value in other operations:

  • It will return an integer representing the memory location of the referenced object, if a number is expected. This allows you to calculate the memory location difference by subtracting one reference from another reference.
  • It will return the object type and memory location in hex of the referenced object, if a string is expected.
  • It will return TRUE, if a Boolean value is expected.

Here is a tutorial program to help you understand this:

#- HardRef5.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
   $a = 3;
   $b = 5;
   $x = '1234';
   $y = 'abcd';
   @l = (3,5,7,11);
   print '\$a = ', \$a, "\n";
   print '\$b = ', \$b, "\n";
   print '\$x = ', \$x, "\n";
   print '\$y = ', \$y, "\n";
   print '\@l = ', \@l, "\n";
   print '\$l[0] = ', \$l[0], "\n";
   print '\$l[1] = ', \$l[1], "\n";
   print '\$b - \$a = ', \$b - \$a, "\n";
   print '\$y - \$x = ', \$y - \$x, "\n";
   print '\$l[1] - \$l[0] = ', \$l[1] - \$l[0], "\n";
#   print '${\$a+36} = ', ${\$a+36}, "\n";

Here is the output of this tutorial program:

\$a = SCALAR(0x1ab2e38)
\$b = SCALAR(0x1ab2e5c)
\$x = SCALAR(0x1ab2e80)
\$y = SCALAR(0x1ab2ea4)
\@l = ARRAY(0x1ab2ed4)
\$l[0] = SCALAR(0x1abf074)
\$l[1] = SCALAR(0x1abf128)
\$b - \$a = 36
\$y - \$x = 36
\$l[1] - \$l[0] = 180

The output shows that the address of $b is higher than $a by 36. But trying to use ${\$a+36} to access $b will not work. I have tried already.

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

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2009
\$b-\$a - Using Hard References in Other Operations