HP Forums

Full Version: question : a trick to call function from a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,
I have a function name in a variable, and I would like to call it, some one know how to do ?
example :
Code:

EXPORT myfct(x)
BEGIN
  PRINT(x);
END;

EXPORT test(fct)
BEGIN
  local call:=fct+"(123)";
  EVAL(call);
END;
Now, If I call test("myfct"), Iwant the function myfct to be called (with parameter 123)
But it does not works as I wanted :
it returns "myfct(123)" but does not execute it...
if someone can help me..
Thank you.
I think
EVAL(EXPR(call))
Or perhaps just
EXPR(call)
Will do what you want.
Hi StephenG1CMZ,
Thank you very much, it's OK.

By the way, a note for thoses who may want to use such callback trick : if you want to use string as function parameters, you have to protect them with backslashs (\").

For example par2 accept string abcd

Code:
EXPORT myfct(par1, par2)
BEGIN
  PRINT(par1+" and "+par2);
END;

EXPORT test(fct)
BEGIN
  local call:=fct+"(123,\"abcd\")";
  EVAL(call);
END;
Reference URL's