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]Thanks

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:

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.

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?

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).