Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks
i need to know how to callback a function from another function.........
sub h { my ($x,$y)=@_; print "the val of x is $#$x and y is $#$y\n"; } sub j ($i, $j, $k){ print "\ni am in the function j\n\n"; &$k($i,$j); } @ar = qw/1 2 3 4/; @vr = qw/56 7 8 9/; &j (\@ar, \@vr, \h);
here subroutine h has to print the number of contents of each array........but this program doesnt work.....

janitored by ybiC: Balanced <code> tags around codeblock, as per Monastery convention

Replies are listed 'Best First'.
Re: using callback function
by antirice (Priest) on Aug 24, 2004 at 04:06 UTC

    A few things:

    1. Check out perldoc perlsub for information about using prototypes and what &j() actually does.
    2. sub j ($i,$j,$k) { should be replaced with sub j { ($i,$j,$k) = @_;
    3. To get a reference to a sub, use \&h.

    Put it all together and you get:

    sub h { my ($x,$y)=@_; print "the val of x is $#$x and y is $#$y\n"; } sub j { my ($i, $j, $k) = @_; print "\ni am in the function j\n\n"; &$k($i,$j); # or $k->($i,$j); } my @ar = qw/1 2 3 4/; my @vr = qw/56 7 8 9/; j(\@ar, \@vr, \&h); __END__

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: using callback function
by BrowserUk (Patriarch) on Aug 24, 2004 at 04:08 UTC

    You need to include the & when you take the reference (otherwise your calling h and then taking a reference to teh result):

    j (\@ar, \@vr, \&h);

    And for most ordinary uses you can drop the & when calling the function as above. To invoke the coderef I prefer to use ->:

    $k->($i,$j);

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: using callback function
by murugu (Curate) on Aug 24, 2004 at 04:17 UTC

    $#$x will print the last subscript value of the refered array. To find number of contents $#$x has to be $#$x + 1.

    Now the subroutine h will be called inside the subroutine j.

    sub h { my ($x,$y)=@_; print "the val of x is $#$x and y is $#$y\n"; } sub j { my ($i, $j, $k)=@_; print "\ni am in the function j\n\n"; &$k($i,$j); } @ar = qw/1 2 3 4/; @vr = qw/56 7 8 9/; &j (\@ar, \@vr, \&h);
Re: using callback function
by ihb (Deacon) on Aug 24, 2004 at 10:32 UTC

    You seem to know some programming but try to brute-force Perl, which doesn't work well with Perl.

    For a quick introduction to Perl, read "perlintro" at www.perldoc.com.

    Futher reading that may help you get started is "perl", "perltoc", "perldata", "perlsub", and "perlref" (also found at www.perldoc.com).

    Other than that, you might want to read a tutorial or two. Just be aware of that there are many IMHO bad tutorial that teaches bad practices.

    ihb

    Read argumentation in its context!