Better way to invoke dynamic function?

H

howa

I have some codes which I want to dynamically select the algorithm at
runtime, currently can be done via using eval(), e.g.
#######################
package P1;

sub foo {
print "foo";
}

sub bar {
print "bar";
}

1;

$function_to_call = "P1.bar";

eval( $function_to_call );

exit;
#######################

But the drawback is eval() is sloooow..., e.g.

##############
cmpthese( 1_000_000, {

'foo' => sub{ foo(); },
'bar' => sub{ eval("foo()"); }

});
##############

result:

bar 19967/s -- -98%
foo 1315789/s 6490% --


as you can see, using eval() is slower a lot, not to mention the
actual function execution time.

Are there any better method to archive runtime algorithm selection?

thanks.
 
J

Joost Diepenmaat

I have some codes which I want to dynamically select the algorithm at
runtime, currently can be done via using eval(), e.g.
#######################
package P1;

sub foo {
print "foo";
}

sub bar {
print "bar";
}

1;

$function_to_call = "P1.bar";

eval( $function_to_call );

Use a function reference:

$function_to_call = \&PI::bar;

$function_to_call->();

Joost.
 
B

Ben Morrow

Quoth Glenn Jackman said:
At said:
On Wed, 05 Dec 2007 06:34:42 -0800, howa wrote:
I have some codes which I want to dynamically select the algorithm at
runtime, currently can be done via using eval(), e.g. [...]
Use a function reference:

$function_to_call = \&PI::bar;

$function_to_call->();

The function name is unknown at runtime, it is input by user.

Presumably the user's input is validated against some number of existing
subroutines ... you want a dispatch table. See
http://www.perlmonks.org/?node_id=456530

e.g.
my %subroutine_refs = (
choice1 => \&PI::bar,
choice2 => \&ALPHA::beta,
);

Note that you can also put the sub definitions directly in the hash,
rather than having to specify the name twice:

my %subrefs = (
choice1 => sub { ... },
choice2 => sub { ... },
);

Ben
 
U

Uri Guttman

h> The function name is unknown at runtime, it is input by user.

that is a VERY bad idea. why do newbies always think allowing users to
input a function name is a good idea?

anyhow the answer you want is a dispatch table. search google groups for
plenty of threads that have discussed them in the past. no reason to
post about them again.

uri
 
H

howa

Uri Guttman $BUmF;!'(B
h> The function name is unknown at runtime, it is input by user.

that is a VERY bad idea. why do newbies always think allowing users to
input a function name is a good idea?

each time user add a new function, you would need to modify the
dispatch table, which might lead to error.
 
U

Uri Guttman

PV> Yes and is a good idea to test if a function is defined

PV> $func_name = 'bar';
PV> if(defined(&{"P1::" . $func_name})) {
PV> (\&$func_name)->(parameters);
PV> }

you are taking a bad idea and making it worse. the answer is a dispatch
table which does the checking AND the dispatching. your answer took the
OP eval code via hard refs and went back to symbolic references. not a
good journey.

uri
 
A

A. Sinan Unur

howa said:
Uri Guttman $BUmF;!'(B


each time user add a new function, you would need to modify the
dispatch table, which might lead to error.

It is deja vu all over again. This topic has been discussed many times
in the past. Search the archives:

http://groups.google.com/group/comp....misc&q=dispatch+table&qt_g=Search+this+group
http://tinyurl.com/2bt5y5

and feel free to learn from past discussion.

Simply stated, both your scheme and objection to Uri's recommendation
are stupid but you should feel free to do as you wish.

Sinan
 
U

Uri Guttman

h> Uri Guttman ÕíÆ»¡§

h> each time user add a new function, you would need to modify the
h> dispatch table, which might lead to error.

gack!! you have users adding functions?? you want that or users calling
anything in your system vs. adding an entry into a dispatch table? i
give up.

uri
 
T

Ted Zlatanov

h> Uri Guttman 寫é“:

h> each time user add a new function, you would need to modify the
h> dispatch table, which might lead to error.

UG> gack!! you have users adding functions?? you want that or users calling
UG> anything in your system vs. adding an entry into a dispatch table? i
UG> give up.

Well hang on, let's not make so many assumptions. Maybe there's a
legitimate reason for howa's need. We should find out what's really
going on.

howa, what are users really selecting through this function? Is it a
loadable module you provide, or is it a function they define for an API
your software provides? How was it done before you used Perl, if at all?

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top