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


In reply to Re: Not Coder reference error - when i pass function by Refernce by huck
in thread Not Coder reference error - when i pass function by Refernce by pks283

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.