References and Data Structures

Using references for passing data to a subroutine

It is recommended to pass data to a subroutine by reference in the following cases:
  1. We want to pass a very large array or hash to the subroutine. If we pass the array or hash variable itself, a whole new copy of the array/hash elements will be created inside the subroutine, thus allocating a double amount of memory. If, however, we pass the variable as a reference, only a pointer to the location of the original hash or array in memory (which is a scalar of type 'reference') is passed to the subroutine.
  2. We want to pass several arrays and/or hashes to the subroutine. By passing them as reference Perl doesn't need to decompose them to simple lists.

In this example, we read PLD1 data from the pld1.txt file; store the data in the $protein_name, @synonyms and %information variables; and print the data in HTML format to the pld1.html file. We pass @synonyms and %information to the print_html subroutine by reference.

#!/usr/bin/perl

#files

open (IN_FILE, $pld1.txt) || die "cannot open "pld1.txt file": $!";

#read data from file

while (<INFILE>) {

   chomp $_;
   my ($field, $value) = split (/:\s+/, $_);

   if ($field =~ /Name/) {          #get protein name
      $protein_name = $value;
   } elsif ($field =~ /Synonym/) {  #get synonyms
      push (@synonyms, $value);
   } else {                         #get information
      $information{$field} = $value;
   }
}
close (IN_FILE);

#call subroutine that prints the data in HTML format.
#we pass the array and hash by reference.

print_html ($protein_name, \@synonyms, \%information, $result_file);

#define subroutine that prints the data in HTML format.

sub print_html {

   #get subroutine parameters. notice that the synonyms and information
   #are received as references, and thus they are stored in scalar variables.

   ($name, $ref_to_synonyms, $ref_to_information, $result_file) = @_;
   
   #dereference $ref_to_synonyms and $ref_to_information

   @synonyms_list = @{$ref_to_synonyms};
   %info_hash     = %{$ref_to_information};

   #open result file

   open (HTML, ">pld1.html") || die "cannot open "pld1.html"; $!";

   #print data to result file in HTML format
   #print top of html page

   print HTML "<HTML><HEAD><TITLE>$name</TITLE></HEAD>\n";
   print HTML "<BODY>\n";

   #print protein name as title

   print HTML "<H3><FONT COLOR='RED'>Human Protein $name</FONT></H3>\n";

   #print synonyms as a list

   print HTML "<H4>Synonyms</H4>\n";
   print HTML "<UL>\n";
   foreach $synonym (@synonyms_list) {
      print HTML "<LI>$synonym\n";
   }
   print HTML "</UL>\n";
   
   #print information as a table

   print HTML "<H4>Information</H4>\n";
   print HTML "<TABLE BORDER CELLSPACING=3 CELLPADDING=4>\n";
   foreach $property (sort keys %info_hash) {
      print HTML "<TR><TD>$property</TD><TD>$info_hash{$property}</TD></TR>\n";
   }
   print HTML "</TABLE>\n";
  
   #print bottom of html page
   
   print HTML "</BODY></HTML>\n";
   close HTML;

}


Table of Contents.
Previous | Next.