CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



gevni
Novice

Dec 1, 2012, 3:57 PM

Post #1 of 6 (3684 views)
Confused in understanding function and it call!! Can't Post

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

Code
sub apply_index (&@)  
{
my $func = shift;
my $array = shift;
return undef unless (defined $func and defined $array);
..........
...........
for($j=0;$j<$card;$j++){
............
............
$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]
Thanks


BillKSmith
Veteran

Dec 1, 2012, 6:07 PM

Post #2 of 6 (3678 views)
Re: [gevni] Confused in understanding function and it call!! [In reply to] Can't Post

(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,
Bill


Laurent_R
Veteran / Moderator

Dec 2, 2012, 2:20 AM

Post #3 of 6 (3674 views)
Re: [gevni] Confused in understanding function and it call!! [In reply to] Can't Post

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:


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


or, maybe:


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


gevni
Novice

Dec 4, 2012, 6:39 AM

Post #4 of 6 (3611 views)
Re: [Laurent_R] Confused in understanding function and it call!! [In reply to] Can't Post

 

Code
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));
$i++;
}
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?


Laurent_R
Veteran / Moderator

Dec 4, 2012, 10:51 AM

Post #5 of 6 (3587 views)
Re: [gevni] Confused in understanding function and it call!! [In reply to] Can't Post

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


gevni
Novice

Dec 4, 2012, 11:13 AM

Post #6 of 6 (3585 views)
Re: [Laurent_R] Confused in understanding function and it call!! [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives