writes:
> Hi all,
>
> I am doing XML processing in perl, i want to read a
> function name from the XML file and call a function with that name at
> runtime. Can I use the "require" statement for it? I was wondering if
> it is possible in perl. Please help me.
You can use eval to do that, but that will allow the XML file to cause
arbitrary code to execute on your machine:
$func = $xml->get_func_name();
eval "${func}()";
You can make that a bit more secure by only allowing word characters,
but it will still allow any function on the system to be called.
You can use symbolic references, which will allow any function to be
called:
{
no strict 'refs';
$func = $xml->get_func_name();
$func->();
}
But the most secure way would be to use "hard references" and make a
hash of allowed functions, mapping names to the reference:
my %allowed_funcs = (
func1 => \&func1,
func2 => \&func2,
);
$func = $xml->get_func_name();
$allowed_funcs{$func} or die "Can't run '$func'";
$allowed_funcs{$func}->();
That gives you precise control over what functions can be called, and
will run just fine under taint mode, "use strict", and "use warnings".
----ScottG.