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.