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

    use strict; use warnings; sub add{ my ($x, $y) = @_; my $sum = $x + $y; print "Sum within function: $sum\n"; return ($sum); } my $bb=\&add(10,1); print $bb."\n"; my $cc=\&add; print $cc."\n";
    Result
    Sum within function: 11 SCALAR(0x3f7e24) CODE(0xa3e58c)
    \&add(10,1) is not a code reference but a reference to the result of &add(10,1)

    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 mm{ my ($x, $y, $add_ref, $counter_ref) = @_; my $cnt_in; my $add_in; my $mul = 1; $mul = ($x * $y * &$add_ref(10,1) * &$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 aa { my $z; $z = mm(4,5, \&add, \&counter); $z = mm(4,5, \&add, \&counter); } aa();
    result
    Sum within function: 11 Value of count in subroutine 1 Multi : 220 Sum within function: 25 Value of count in subroutine 2 Multi : 1000 Sum within function: 11 Value of count in subroutine 3 Multi : 660 Sum within function: 25 Value of count in subroutine 4 Multi : 2000

      Thanks for feedback

        FYI,
        There is an alternate syntax using the arrow operator when calling a reference to a sub, e.g.:
        #$mul = ($x * $y * &$add_ref(10,1) * &$counter_ref()); $mul = ($x * $y * $add_ref->(10,1) * $counter_ref->());
Re: Not Coder reference error - when i pass function by Refernce
by Anonymous Monk on Feb 25, 2017 at 23:11 UTC
    \&add(10,1) is not a reference to sub add with the parameters, it's \( &add(10,1) ), so a reference to the return value of &add(10,1), so \11.

    If you want to do something like Currying, write sub { add(10,1) } instead.