pks283 has asked for the wisdom of the Perl Monks concerning the following question:
Hi, i need help with below implementation of passing function by reference. I am passing add function to multiply function. It executes first time correctly, but fails next time executing function passed by reference. I have played with code and not able to figure out what i am missing. Below is my code.
use strict; use warnings; our $static_counter = 1; sub add{ my ($x, $y) = @_; my $sum = $x + $y; print "Sum within function: $sum\n"; return ($sum); } sub counter { my $cnt = $static_counter++; print "Value of count in subroutine $cnt\n"; return($cnt); } sub multiply{ my ($x, $y, $add_ref, $counter_ref) = @_; my $cnt_in; my $add_in; my $mul = 1; $mul = ($x * $y * $$add_ref * $counter_ref->()); print "Multi : $mul\n"; $add_in = $add_ref->(20,5); ### Getting Error Here ### Not a Code Reference $mul = ($x * $y * $add_in * $counter_ref->()); print "Multi : $mul\n"; return $mul; } sub a { my $z; $z = multiply(4,5, \&add(10,1), \&counter); $z = multiply(4,5, \&add(10,1), \&counter); } a();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Not Coder reference error - when i pass function by Refernce
by huck (Prior) on Feb 25, 2017 at 22:34 UTC | |
by pks283 (Novice) on Feb 25, 2017 at 23:16 UTC | |
by Marshall (Canon) on Feb 26, 2017 at 21:09 UTC | |
by stevieb (Canon) on Feb 26, 2017 at 21:36 UTC | |
|
Re: Not Coder reference error - when i pass function by Refernce
by Anonymous Monk on Feb 25, 2017 at 23:11 UTC |