Home: Perl Programming Help: Intermediate:
Confused in understanding function and it call!!


Dec 1, 2012, 3:57 PM

Views: 6189
Confused in understanding function and it call!!

I have a function. And I won't be able to understand logic of some lines, what they are doing.

sub apply_index (&@)  
my $func = shift;
my $array = shift;
return undef unless (defined $func and defined $array);
$i = 0;
for($i = 0; $i <= $#{$array}; $i++){
&$func($j) if defined $res;
return 0;

#Function Call
my @index;
Math::Permute::Group_Array::Apply_on_index {push @index, @_} [0..$size_col[$i]-1] ;

I don't understand these 4 lines of this function.
(1) sub apply_index (&@) (What is & operator mean here)
(2) &$func($j) if defined $res;
(3) return 0;
(4) Math::Permute::Group_Array::Apply_on_index {push @index, @_} [0..$size_col[$i]-1] ;

At 1 is it passing the reference of array? (But in perl we never use such & symbol for reference passing)???
At 4 Its not like a normal function call, I am confused about {push @index, @_} [0..$size_col[$i]-1]


Dec 1, 2012, 6:07 PM

Views: 6183
Re: [gevni] Confused in understanding function and it call!!

(1) The highlighted characters form a function prototype. They do not mean what users of other languages think they do. They should never be used unless you are sure that they are needed. Remove them.

(2) $func must be a reference to a subroutine (passed in through the argument list). This line is a call to that subroutine.

(3) Return from the subroutine immediately with a return value of zero.

(4) Refer to the documentation for this module.
Good Luck,

Veteran / Moderator

Dec 2, 2012, 2:20 AM

Views: 6179
Re: [gevni] Confused in understanding function and it call!!

Same idea as Bill.

1. You should probably remove the prototype (the '(&@)' part) in the function definition unless you know damn well why you are doing it.

2. &$func is a code ref or a reference to a function. It is passed to the apply_index function, which will be able to use it. This is what if usually called a callback function.

You have probably already met such callbacks, although possibly without really noticing. The most common example is when you are using the sort function. If you want to sort an array or a hash by its values, you can write something like:

my @sorted_result = sort {$a <=> $b} @unsorted;

or, maybe:

my @sorted_result = sort {$day_income{$a} <=> $day_income{$b}} keys %day_income;

In both cases, you are really passing a reference to some code to the sort function, so that it knows what comparison subroutine or code it should use for sorting your data. This is the possibility to do that which makes the sort function so useful: sort will do the sorting using its own efficient sorting algorithm and applying your own sorting criteria.

This is just an example which is built-in within Perl. You can also define your own functions to be passed to other functions, making your code far more flexible.

For example, you can write a generic function in a package or a module that makes some complicated calculation. But you may want the result of this calculation to be printed out to the screen, stored into some variable, data structure or object, returned to the calling function, checked for consistency with other data or even used for some further calculation. Rather than trying to plan every possible use that the user of your function will want to apply, you can simply pass to the generic function a reference to another (used-defined) function, that the generic function will apply to the results of its own calculation.

This is what happens in the code snippet you have posted. The
"{push @index, @_}" code ref is passed to the apply_index function, which does not know what this code is doing, but will blindly apply it to the result of its own computations when executing the "&$func" code.


Dec 4, 2012, 6:39 AM

Views: 6116
Re: [Laurent_R] Confused in understanding function and it call!!


sub permutation 
my $self = shift;
my $rest = shift;
print "$self\n";
print "$rest\n";
my @array = @{$self->{array}};
my @res;
my $i = 0;
while($rest != 0){
$res[$i] = splice @array, $rest % ($#array + 1), 1;
$rest = int($rest / ($#array + 2));
push @res, @array;
return \@res;

#Function Call
my $value= $permutor[$j]->permutation($perm->[$j]);

let say $perm->[$j] = 4.I try to do it hand execution for understanding but didn't get what is inside $rest and what that statement is doing $res[$i] = splice @array, $rest % ($#array + 1), 1;
$rest = int($rest / ($#array + 2));

It looks like that it is some OOP programming. Which i didn't get. Can some one explain me what this function is doing inside? and what would be the output?

Veteran / Moderator

Dec 4, 2012, 10:51 AM

Views: 6092
Re: [gevni] Confused in understanding function and it call!!

splice @ARRAY, OFFSET [ , LENGTH [ , LIST ] ] : Removes the elements of @ARRAY designated by OFFSET and LENGTH,
and replaces them with LIST (if specified). Returns the elements removed.

In your case, you don't have a list, length is 1 and offset is '$rest % ($#array + 1)'.

$#array is the index of the last element of your array and $#array + 1 is the size of the array.

So, '$rest % ($#array + 1)' calculates the rest of the division of $rest by the size of the array.

Assume you array has a size of 20 and $rest is 23.

$rest % ($#array + 1) will be 23%20 = 3. The statement will remove the 3rd element of the array and put it into $res.

Array size is now 19.

Next, $rest is set to : int($rest / ($#array + 2)) = int (23 / 21) = 1.
$rest % ($#array + 1) will now be 1 % 19 = 1. The first element of the array will be removed and put into @res.

Array size is not 18.

$rest is set to: int (1 / 20) = 0.

$rest being 0, you now exit the while loop.


I do not know at this point what this is aiming at because I have no idea of what you have in @array and what the original value of $rest is, but, at least you can unroll the algorithm (I hope I did not make a silly error in doing it, I just did it quickly, and did not try it out, but you should get the idea).


Dec 4, 2012, 11:13 AM

Views: 6090
Re: [Laurent_R] Confused in understanding function and it call!!

Thank, Thank, Thanks, I get it now. You are really a good teacher :)