Gossamer Forum
Home : General : Perl Programming :

function pointer assignment syntax

Quote Reply
function pointer assignment syntax
Greetings all perl programmers. I am a recent addition to your ranks and as such have a question that may just be a matter of syntax.

I wish to populate a hash with a package. In package is a function pointer and a error string, the function or sub in perl's case needs to take a list

in c I would do this:

typedef struct FunctionInfo {

bool (*fpFunction)(List *spList);

char *cpRetVal;

}FunctionInfo, *pFunctionInfo;

then I would create a list of these with a key using a char str



Now in perl I know there are packages though they are different in declaration and all of that.

However the assignment of the function pointer is where things get a little fuzzy for me.



if I have some sub



sub Action(@ActionList)

{

}



and I want a function pointer to that

$fpAction = Action;



from what I have experienced this executes the function, in which case I use single quotes

$fpAction = 'Action'; (though for clarity I use &Action, but it should be the same);



then for using the function pointer the book I have says to use &$fpAction but this yields an undefined sub.



So I am a lilttle confused as to the function pointer assignment syntax.
Quote Reply
Re: [djorlett] function pointer assignment syntax In reply to
Using &$sub is for code references and anonymous sub-routines. I think you want something like this:

Code:
sub your_sub {

my (@arguments) = @_;

.. do stuff ..

return $something;
}

my $returned_value = your_sub(@arguments);
Quote Reply
Re: [djorlett] function pointer assignment syntax In reply to
Hi,

If you want a reference to a subroutine:

sub foo { print "in foo\n"; }

my $func_ref = \&foo;
$func_ref->();

$func_ref is your pointer, and to execute the sub, you do $func_ref->('any args');

Your terminology is a bit off too with respect to packages. A package just defines a namespace -- so if you have package foo; and a sub bar inside that package, then from outside of the package, you would reference it foo::bar().

Cheers,

Alex
--
Gossamer Threads Inc.

Last edited by:

Alex: Jun 2, 2003, 11:06 AM
Quote Reply
Re: [Paul] function pointer assignment syntax In reply to
well that will give me what my sub is returning. However I want the actual block of memory where the function is



ie where $fpAction and Action are the same

basically I want to call the scalar as some sub that is assigned.



to explain it by example, because I don't think I am doing a good job at explaining this.



I have these actions

eat, sleep, drink, do_work, be entertained, and work_out

Those actions are placed into an action list. I also have a list of keys for which I use in a hash for what action the object will peform



I have a list of objects that are "active" and those process their actions.



now I could do this

Code:


foreach $newAction (@ActionList) {

if($newAction eq "eat") {

Eat();

} else if($newAction eq "sleep") {

}

}


However that is innefficent if there are a number of actions, as well as cumbersome to add to in the long run.

if I could simply do this

foreach $action (@ActionList) {

%ActionTable{$action}(args);

}

that would remove all unneeded checking and all I would have to do is append another action to the table, and have that table stored in some init file, or better yet have that table generated off of some database



my problem is I do not know how to assign the function to an variable

again in c its pretty simple

void Function(int i)

{

printf("%d", i);

}



void main()

{

// declare function pointer

void (*fpFunction)(int i);

// some random int

int iNewInt = 10;



// assign the function pointer to a function

fpFunction = Function;



// call the function pointer

fpFunction(iNewInt);

}
Quote Reply
Re: [Alex] function pointer assignment syntax In reply to
Right on! that is what I was looking for thanks
Quote Reply
Re: [djorlett] function pointer assignment syntax In reply to
Ok now I see. In that case Alex's suggestion is the one you need. You can also do it like:

Code:
my $code_ref = sub { return "Hello!" };

If you then print $code_ref you'll see the memory location.
Quote Reply
Re: [Paul] function pointer assignment syntax In reply to
Right on gents thanks for the quick responce!