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

\$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.

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
\$b-\$a - Using Hard References in Other Operations