Solved

Function pointer equivalent

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 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