Solved

Function pointer equivalent

Posted on 2008-06-20
3
765 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:DJ_AM_Juicebox
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 21830301
sub test (&@)
    {
        my ($function, @data) = @_;
        &$function(@data);
    }
  sub saysomething
    {
         print("hello @_\n");
    }


test(\&saysomething, "my","array");
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21830877
Why do you have the:

    \&

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

Thanks
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21831138
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 \&.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help with bash and/or perl commands on OS X Terminal 9 110
PERL - Find newest folder 12 162
Ned Perl Snippet to Read Files in Directory 5 79
parse a file and get data out 11 103
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question