Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Function pointer equivalent

Hi,

I'm trying to figure out this perl syntax for passing a function 'pointer'.

    sub test (&@)
    {
        my ($function, @data) = @_;
    }

so do the two egyptian hieroglyphs mean that the sub only takes a sub, then an array as parameters? How exactly do I pass a sub to a call of test(), like:

    sub saysomething()
    {
         print("hello\n");
    }

    test(saysomething, @myarray);


If that's right, how can I then call $function once it's passed to test, and even pass a parameter to it?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Why do you have the:

    \&

before saysomething, I left it out and it still seems to work ok - what is the effect?

Thanks
The \& returns a reference to the subroutine.  If you have the use strict and use warnings options turned on, it will not work without the \&.