|
perlref - Perl References and Nested Data Structures
Part:
1
2
3
(Continued from previous part...)
Creating Hard References
Hard references are pointers (or addresses) of other variables, functions, or any
objects. Hard references are stored in scalar variables. They can be created in
several ways.
1. Using the backslash operator, \, on a variable or a function to create a hard
reference for that variable or function, as in the following examples:
$r1 = \$a;
$r2 = \@b;
$r3 = \%c;
$r4 = \&d;
There are three special usages of the backslash operator, \:
1.1. Using the backslash operator, \, on an expression that returns a scalar to
create a hard reference to an anonymous scalar (not assigned to any variable name),
as in the following examples:
$r1 = \1;
$r2 = \'foo';
$r3 = \(1+1);
1.2. Using the backslash operator, \, on an list object enclosed in parentheses to
create a list of hard references to all elements in the list object, as in the
following examples:
@r1 = \(Mon, Tue, Wed); # same as (\'Mon',\'Tue',\'Wed')
@r2 = \(@b); # not the same as \@b
1.3. Using the backslash operator, \, on a hard reference to
create a hard reference of the specified hard reference, as in the
following examples:
$r1 = \$a;
$r2 = \$r1;
$r3 = \\\%c;
2. Using square brackets on a list object to create a hard reference for an anonymous
array (not assigned to any variable name), as in the following examples:
$r1 = [3,5,7,11];
$r2 = [Mon, Tue, Wed];
$r3 = ['Jan',31,'Feb',28,'Mar',31];
3. Using curly brackets on a list object to create a hard reference for an anonymous
hash (not assigned to any variable name), as in the following examples:
$r1 = {3,5,7,11}; # not very useful
$r2 = {Mon, Tue, Wed}; # not very useful
$r3 = {'Jan',31,'Feb',28,'Mar',31};
4. Using "sub" without a name to create a hard reference for an anonymous function
(not assigned to any function name), as in the following examples:
$r1 = sub {print "I am here.\n";}
Using Hard References
Similar to soft (symbolic) references, there several syntax formats to use hard references:
1. Directly placing a scalar variable that contains the hard reference in the
place where the identifier should be. The following program shows you some examples:
#- HardRef1.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
$foo = 0;
@foo = (0);
%foo = (k,0);
$refs = \$foo;
$refa = \@foo;
$refh = \%foo;
$reff = \&foo;
$$refs = 10; print "$foo\n"; # the scalar of $foo
@$refa = (20); print "$foo[0]\n"; # entire array of @foo
$$refa[0] = 30; print "$foo[0]\n"; # an element of @foo
@$refa[0] = (40); print "$foo[0]\n"; # a slice of @foo
%$refh = ('k',50); print "$foo{k}\n"; # entire hash of %foo
$$refh{'k'} = 60; print "$foo{k}\n"; # an element of %foo
@$refh{'k'} = (70); print "$foo{k}\n"; # a slice of %foo
&$reff(80); sub foo {print "$_[0]\n";} # calling &foo(80)
Please note that references take higher precedence that subscriptions: [], lookups: {},
and function calls: (). For example, $$refa[0] will be evaluated
$$refa fist, not $refa[0] first.
2. Placing an expression that returns a hard reference in curly brackets {}
to replace the identifier.
(Continued on next part...)
Part:
1
2
3
|