References And Dereferences

                                                                                                                         

 

References & Dereferences

What is References & Dereferences :

  • Reference is same thing in PERL as pointer in C/C++.
  • So a reference is a special form of scalar variable in PERL that stores the location (address) of a data structure.

A reference is a scalar value that points to a memory location that holds some type of data.

OR

we can say that References are used to hold the memory addresses.

AND

When a reference is dereferenced, you retrieve the information referred to by the reference.

Why to use References & Dereferences :

  • References are useful in creating complex data structures in PERL.
  • It allows you to change the variable the reference points to (and hence the value it points to in that variable’s memory location ).
  • Can easily handles the multiple list and hashes.

 

Creating reference :

  • To create a reference, prefix the \ operator to the data object.

Example:

$variable = 22;

$pointer = \$variable;;

 

Reference & Dereferences of various variables:

Reference assignment How to do dereference
     
1) $refScalar = \$scalar; ${$refScalar} is a scalar value.
2) $refArray = \@array; @{$refArray} is an array value.
3) $refHash = \%hash; %{$refHash} is a hash value.
4) $refFunction = \&function; &{$refFunction} is a function location.
5) $refGlob = \*FILE; $refGlob is a reference to a file handle and
seems to be automatically dereferenced by
 6)  $refRef = \$refScalar; ${${$refRef}} is a scalar value.

Array Reference & Dereferences :

  • Reference:

sub add_numbers

{ my $array_ref = shift;

…..

}

@numbers = (11,2,3,45);

$array_ref = add_numbers(\@numbers);

    • We can take the reference of an Anonymous Array into the scalar variable as shown below. $array_ref = [ 11,2,3,45];
  • Dereference:
    • Inside the subroutine, the dereference can be done by using as below.

@{ $array_ref };

    • To obtain a particular element in an array

$ { $array_ref }[0]

 

  • Reference:

sub add_numbers

{ my $array_ref = shift;

…..

}

@numbers = (11,2,3,45);

$array_ref = add_numbers(\@numbers);

    • We can take the reference of an Anonymous Array into the scalar variable as shown below. $array_ref = [ 11,2,3,45];
  • Dereference:
    • Inside the subroutine, the dereference can be done using below method:

@{ $array_ref };

    • To obtain a particular element in an array

$ { $array_ref }[0]

Example:

#!/usr/bin/perl

# Using Array references

$pointer = \@ARGV;

printf “\n Pointer Address of ARGV = $pointer\n”;

$i = scalar(@$pointer);

printf “\n Number of arguments : $i \n”; $i = 0;

foreach (@$pointer) {

printf “$i : $$pointer[$i++]; \n”;

}

Result:

$ test 3 5 4

Pointer Address of ARGV = ARRAY(0x00330488) Number of arguments : 3

0 : 3;

1 : 5;

2 : 4;

 

Hash Reference & Dereferences :

Example:

#!/usr/bin/perl

# Using Hash references

%days = (’01’, ‘Sun’,’02’, ‘Mon’, ’03’, ‘Tues’, ’04’, ‘Wed’, ’05’,‘Thru’, ’06’,’Fri’,’07’,’Sat’);

$pointer = \%days;

printf “\n Address of hash = $pointer\n “;

# The reference to the associative array via $pointer

foreach $i (sort keys %$pointer) {

printf “$i is $$pointer{$i} \n”;

}

Result:

Address of hash = HASH(0x806c52c) 01 is Sun

02 is Mon

03 is Tues

04 is Wed

05 is Thru

06 is Fri

07 is Sat

 

Multidimensional Arrays:

  • You create a reference to an array through the statement @array = list.

Example:

$line = [‘solid’, ‘black’, [‘1′,’2′,’3’] , [‘4’, ‘5’, ‘6’]];

  • The preceding statement constructs an array of four elements. The array is referred to by the scalar $line.
  • To get to the elements of the inner array elements:

$arrayReference->[$index] 1-D array

$arrayReference->[$index1][$index2] 2-D array

$arrayReference->[$index1][$index2][$index3] 3-D array

 

Example:

#!/usr/bin/perl -w

$line = [‘solid’, ‘black’, [‘1′,’2’] , [‘4’, ‘5’]];

print “\$line->[0] = $line->[0] \n”; print “\$line->[1] = $line->[1] \n”;

print “\$line->[2][0] = $line->[2][0] \n”; print “\$line->[2][1] = $line->[2][1] \n”;

print “\$line->[3][0] = $line->[3][0] \n”; print “\$line->[3][1] = $line->[3][1] \n”;

Result:

$line->[0] = solid $line->[1] = black $line->[2][0] = 1 $line->[2][1] = 2 $line->[3][0] = 4 $line->[3][1] = 5

References to Subroutines:

  • To construct subroutine reference, you use the following type of statement: $sub_ref= sub {statements….} ;
  • Use of the semicolon at the end of the sub declaration.
  • The subroutine is referred to by $sub_ref points to the same function reference even if this statement is placed in a loop.
  • This feature of Perl enables you to declare anonymous sub() functions in a loop without worrying about whether you are chewing up memory by declaring the same function over and over.
  • To call a subroutine by reference, you must use the following type of reference:

&$sub_ref( parameters );

 

  • This code works because you are de-referencing the $sub_ref and using it with the ampersand (&) as a pointer to a function.
  • The parameters portion might or might not be empty depending on how your function is defined.

Example:

#!/usr/bin/perl

sub print_coor{

my ($x,$y,$z) = @_; print “$x $y $z \n”; return $x;

};

Result:

1 2 3

4 5 6

$k = 1; $j = 2; $m = 4;

$this = print_coor($k,$j,$m); $that = print_coor(4,5,6);

 

Reference to multiple array:

Eaxmple :

#!/usr/bin/perl

@names = (abc,pqr,xyz);

@phones = (5551234, 5554321, 666 );

$i = 0;

sub listem {

my ($a,$b) = @_;

foreach (@$a) {

print “a[$i] = ” . @$a[$i] . ” ” .

“\tb[$i] = ” . @$b[$i].”\n”;

$i++;

}

}

&listem(\@names, \@phones);

Result:

a[0] = abc b[0] = 5551234
a[1] = pqr b[1] = 5554321
a[2] = xyz b[2] = 666

 

References to File Handles:

  • Sometimes, you have requirement to write the same output to different output files also.
  • Rather than make separate statements for each handle, it would be nice to write something like the following:

Asterisk used for wildcard entry

spitOut(\*STDIN);

spitOut(\*LPHANDLE);

spitOut(\*LOGHANDLE);

  • That the file handle reference is sent with the \*FILEHANDLE syntax because you refer to the symbol table in the current package.
  • In the subroutine that handles the output to the file handle, you would have code that look something like the following:

sub spitOut {

my $fh = shift;

print $fh “Gee Wilbur, I like this lettuce\n”;

}

 

References to reference:

You can set up a reference to a reference, although you won’t need this type of ability until you get into complex coding:

$num1=10;

$refnum1=\$num1;

$refnum2=\$refnum1;

  • The last line sets $refnum2 to the value of $refnum1 (the memory location of $num1), and not to $num1 directly. To dereference $refnum2 here and see the value of $num1, use:

print $$$refnum2;

                                                                                                                         

 

Leave a Reply

Your email address will not be published. Required fields are marked *