12.1.6 Other Type Specifiers
12.1.6 Other Type Specifiers
In specifying the type of the parameters, Perl allows us to use the following characters: $ @ % * and &. @ stands for a list variable or list value, % stands for a hash variable or hash value, & stands for an anonymous subprogram and * stands for a reference to a symbol table entry. We can precede the $, @ and % by a \
to send a reference to the variable of the required kind. In such a case, in invocations of the subprogram, the “actual parameter” has to start with the specified character.
As a last example, the following is a function that takes two list variables and optionally four scalar values or variables. The first two scalars are used as the start and end indices for the first list to pick out a substring. If the first scalar is not given, it is assumed to be 0. If the second scalar is not given, it is assumed to be the last index in the first list. Similarly, the third and the fourth scalars are taken to be the start and end indices of the second list so that a relevant substring can be picked out. The function appendSubSeq takes the specified sublist of the first list, and the specified sublist of the second list and appends them together and returns this appended list. If for any list, the start index is given to be bigger than the end index, no subsequence is picked from the list.
Program 12.6
#Here is a function that takes two lists followed optionally
#by between zero and four numeric scalars.
#The two lists must be provided as list variables.
sub appendSubSeqs (\@\@;$$$$){
my ($firstListRef, $secondListRef, $start1, $end1,$start2, $end2) = @_;
my @firstList = @$firstListRef;
my @secondList = @$secondListRef;
unless (defined ($start1)) {$start1 = 0;}
unless (defined ($start2)) {$start2 = 0;}
unless (defined ($end1)) {$end1 = $#firstList;}
unless (defined ($end2)) {$end2 = $#secondList;}
return (@firstList[$start1..$end1], @secondList[$start2..$end2]);
}
Having written the subprogram above, we can instantiate @list1 and @list2 in to various values and see how appendSubSeqs works. Here are some possible calls to appendSubSeqs.
@list1 = (1, 2, 3, 4); @list2= (10, 11, 12, 13, 14);
@result = appendSubSeqs (@list1, @list2);
@list1 = (1, 2, 3, 4); @list2= (10, 11, 12, 13, 14);
@result = appendSubSeqs (@list1, @list2, 2);
@list1 = (1, 2, 3, 4); @list2= (10, 11, 12, 13, 14);
@result = appendSubSeqs (@list1, @list2, 2, 3);
@list1 = (1, 2, 3, 4); @list2= (10, 11, 12, 13, 14);
@result = appendSubSeqs (@list1, @list2, 2, 3, 2);
@list1 = (1, 2, 3, 4); @list2 = (10, 11, 12, 13, 14);
@result = appendSubSeqs (@list1, @list2, 2, 3, 2, 4);
#If a list is empty, no subsequence is picked
#even if the two indices are given.
@list1 = (); @list2 = (10, 11, 12, 13, 14);
@result = appendSubSeqs (@list1, @list2, 2, 3, 2, 4);
@list1 = (); @list2 = ();
@result = appendSubSeqs (@list1, @list2, 2, 3, 2, 4);
#if start is bigger than end for a list, no subsequence is picked
@list1 = (1, 2, 3, 4); @list2 = (10, 11, 12, 13, 14);
@result = appendSubSeqs (@list1, @list2, 3, 1, 2, 4);
