Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Is this possible in perl?

Reply
Thread Tools

Is this possible in perl?

 
 
madhav_a_kelkar@hotmail.com
Guest
Posts: n/a
 
      12-01-2004
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.


Thanks,

Madhav.
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      12-01-2004
wrote:

> 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.


Use a dispatch table. Construct a hash of code refs:

my %dispatch = (
do_this => \&this,
do_that => \&that,
do_other => \&other,
);

And then make your function call by way of the dispatch table:

# Assuming $func has been read from XML input,
if (exists $dispatch{$func}) {
$dispatch{$func}->();
} else {
# No function found for $func, so handle the error
}

See 'perldoc perlref' for lots of details. There is also an example in
'perldoc perlfaq7', at the end of the answer for "How do I create a
switch or case statement?"

> Can I use the "require" statement for it?


What leads you to believe that require() has anything to do with calling
a function? See 'perldoc -f require'.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-01-2004
wrote:
> 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.


Typically you would use a dispatch table.

You could also use eval(), but of course depending upon where your XML file
is coming from this may open a major security hole.

> Can I use the "require" statement for it?


Maybe in some convoluted way. require() is certainly not something that
would come to my mind for calling a function.

jue


 
Reply With Quote
 
Mikael Andersson
Guest
Posts: n/a
 
      12-07-2004
wrote:
> 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 could create A package, lets call it Funcs.pm, and implement all you
functions there. And then do something similar to this.

use Funcs;

$toCall ="Funcs::$functionNameFromXML";
&$toCall;

/Mikael
 
Reply With Quote
 
Scott W Gifford
Guest
Posts: n/a
 
      12-07-2004
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.
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      12-09-2004


Scott W Gifford wrote:

> 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".


You can keep the precise control and avoid the uglnessess of having to
list all the allowed funcs thrice by simply defining all the allowed
functions with a package prefix that you do not use for anything else.
This way you use get Perl to put the functions directly into a dispatch
table called %My::Module::XMLfunc:: and avoid the need to copy them into
another hash.

sub My::Module::XMLfunc::func1 {
# do stuff...
}

#...
{
no strict 'refs';
$func = $xml->get_func_name();
"My::Module::XMLfunc::$func"->();
}

 
Reply With Quote
 
Peter Scott
Guest
Posts: n/a
 
      12-10-2004
In article <cpa5rv$d94$>,
Brian McCauley <> writes:
>Scott W Gifford wrote:
>> 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".

>
>You can keep the precise control and avoid the uglnessess of having to
>list all the allowed funcs thrice by simply defining all the allowed
>functions with a package prefix that you do not use for anything else.
>This way you use get Perl to put the functions directly into a dispatch
>table called %My::Module::XMLfunc:: and avoid the need to copy them into
>another hash.
>
>sub My::Module::XMLfunc::func1 {
> # do stuff...
>}
>
>#...
> {
> no strict 'refs';
> $func = $xml->get_func_name();
> "My::Module::XMLfunc::$func"->();
> }


Why not

{
my $func = $xml->get_func_name();
My::Module::XMLfunc::{$func}->();
}

? Then you can use the stash for its hash advantages.

--
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      12-11-2004


Peter Scott wrote:

> In article <cpa5rv$d94$>,
> Brian McCauley <> writes:
>
> {
>> no strict 'refs';
>> $func = $xml->get_func_name();
>> "My::Module::XMLfunc::$func"->();
>> }

>
>
> Why not
>
> {
> my $func = $xml->get_func_name();
> My::Module::XMLfunc::{$func}->();
> }


Well appart from the missing $ there's nothing wrong with that.

However I kinda prefer manipulations of the symbol table to look like
what they are.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
efficient data loading with Python, is that possible possible? igor.tatarinov@gmail.com Python 10 12-14-2007 04:44 PM
is it possible to possible to create an iterator from a callback interace? aninnymouse@gmail.com C Programming 4 02-21-2006 02:10 PM
XML + XSD: Is it possible to get all possible Values for an Element? Markus Java 1 11-22-2005 02:51 PM
Possible to connect 2 computers via USB? Michael Giroux Wireless Networking 2 09-02-2004 03:27 AM
Possible domain related wireless problem =?Utf-8?B?QWxpIEQ=?= Wireless Networking 0 08-10-2004 01:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57