Solved

Function pointer equivalent

Posted on 2008-06-20
3
761 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now