SP> No, actually I've been away from usenet for quite a while.
SP> I can imagine some complications - there are security issues if
SP> you don't check the incoming sub name carefully, and you'll want
SP> to verify that the requested sub exists, of course. Nothing I'd
SP> describe as "horrible," although I'll concede that it might be
SP> dangerous in the hands of a newbie, or someone for whom defensive
SP> & secure programming isn't normal practice.
if you don't feel those possibilities are horrible, then you shouldn't
be answering stuff here. this is obviously a newbie and just the type to
not tell about symrefs and eval. we have a group rule about not doing
that. they don't need those techniques (in fact very few do) and they
are inherently dangerous. even if this script is isolated and run only
by this kid, he will learn a technique early on and will use again and
again. his brane is now infected with mad perl code disease.
SP> Something like this?
SP> my $disp = {
SP> 'foo' => \&foo,
SP> 'bar' => \&bar,
SP> 'baz' => sub { return 'baz'; },
SP> };
SP> my $funcName = 'foo';
SP> my $result = &$disp->{$funcName};
SP> That's useful if you want to limit the caller to a known set of
SP> "approved" functions. But nothing in the OP's question indicated a
SP> desire/need to limit the functions that could be called. I simply
SP> assumed he was aware of the consequences of executing arbitrary
SP> code, and answered the question as given.
never just answer a newbie question as a given. this is true for all
teaching to young ones. they usually don't know enough to ask the right
questions let alone figure out the answers. most times they have no
understanding of the evils of things and are just happy to get it
working. notice the OP (under a different name?!) responded to your post
with a 'it works' comment. so he is using one of your answers. and i
doubt he will be reading the rest of this thread.
SP> In hindsight, I think you're right - a caveat was in order, and a
SP> safer option that's preferred whenever it isn't too restrictive.
that was my point. or better, never mention them unless the situation
warrants it. brian did mention both and then showed a basic dispatch
table. i should rebuke him for even showing those first. newbies seem to
like the concept of symrefs as it satisifies their craving for something
that works and who cares about any side effects. they don't realize the
symtable is just a special and dangerous hash tree and so why not use a
regular data structure - the dispatch table. most don't know about hard
refs or code refs and using hashes for verifying entry data (one of the
nice side effects of dispatch tables) is also a new concept to most.
SP> I'd love to, but I don't think Pink Floyd is ever going to tour
SP> again. ;-(
i have totally forgotten the july 3, 1977 concert in madison square
garden where they did all of animals and all of wish you were here with
amazing synchronized animation and effects. encore were a few songs from
dark side. i wish i was still there.
uri