Thursday, November 29, 2012
11:00 PM

Perl Script: Creating pointers (References) to variable, Array and Hash

A reference is a scalar value that points to a memory location that holds some type of data. Everything in your Perl program is stored inside your computer's memory. Therefore, all of your variables, array, hash and functions are located at some memory location. References are used to hold the memory addresses.

Below is simple Perl script which demonstrate the usage of reference ...

Source: cat pointer.pl
#!/usr/bin/perl

# Creating pointer to a variable.
$var = 10;
$pointer = \$var;
print "Pointer address is: $pointer \n";
print "value store at $pointer is $$pointer \n";

print "--------------------------------\n";

# Creating pointer to an array.
@array = ("1", "2", "3", "4");
$array_pointer = \@array;
print "Address of the array in the memory: $array_pointer \n";

$len = scalar(@$array_pointer);
print "Length of the array: $len \n";

for ($i=0; $i<$len; $i++) {
  print "$$array_pointer[$i] \n";
}
Continue Reading...

0 comments:

Post a Comment