Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Function pointer equivalent

Posted on 2008-06-20
3
Medium Priority
?
768 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 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

Independent Software Vendors: 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

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…
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…
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…
Six Sigma Control Plans

688 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