in reply to Pass sub reference

For example:

#!/usr/bin/perl sub xxx { my $x1 = shift; my $x2 = shift; print "$x1, $x2\n"; } sub yyy { my $func = shift; $func->(@_) } yyy( \&xxx, "foo", 42 ); # \&xxx is the sub reference.

Replies are listed 'Best First'.
Re^2: Pass sub reference
by Anonymous Monk on Oct 28, 2010 at 17:16 UTC
    ...or maybe, if you want to pass around the arguments together with the function, you can wrap the call in another sub:

    ... sub yyy { my $func = shift; $func->(); } yyy( sub { xxx("foo", 42) } );

      Which is to say (warning: semi-advanced topic, but not really all that scary): Use a closure. See tutorial Closure on Closures.