LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-02-2007, 09:32 AM   #1
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
[perl] Convert String to Function Reference


Say I've got some subroutines with names like add, subtract, and multiply. And I want to be able to read in strings from the user, then do the appropriate function. I know that one possible solution is to build a map from the name of the function (as a string) to a function reference. But that means I have to build a map and keep it updated. Is there any quick way to automagically convert a function name into a function reference for the subroutine of that name? Or in other words, is there any function that takes a string and returns a function reference?
 
Old 10-02-2007, 10:26 AM   #2
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 509

Rep: Reputation: 41
I guess, "autoload" is what you search for.
 
Old 10-02-2007, 11:58 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
You can use eval, e.g. on the command line:
Code:
perl -ne 'chomp; print eval($_) . "\n";'
However - be very very careful - if the user can pass arbitrary strings to eval, your program can be used to execute anything which the user passes... Do not use this method if the input is coming from an untrusted source or the process is running with any sort of escalated priviliages. An example of how nasty this can be... imagine the user passing the string:
Code:
system("rm -rf /");
You have been warned!
 
Old 10-02-2007, 12:24 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Original Poster
Rep: Reputation: 40
Su-Shee: Do you mean the AUTOLOAD subroutine? I don't see immediately how this can help. Could you give an example?

matthewg42: That could work, I suppose, if there's no better way... Security isn't really a problem here, but it would be bad for somebody to accidentally trash their data, far-fetched as that may seem.

I was hoping more along the lines of something I've done before (I think) in Lisp, where you can take a string and turn it into an interned symbol that refers to a function. Maybe it was something else, but I'm pretty sure that I had done it like that.
 
Old 10-02-2007, 01:26 PM   #5
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 509

Rep: Reputation: 41
Yes.

AUTOLOAD trys to load whatever you pass to it - meaning, if your user would just call "add", autoload would load any "add" you assigned to it. If your user calls "grmhbfmpf", autoload will try to call that and because you've got a nice default and could catch such things, the user would get a polite error message or a default instead.

Alternatively, you could do your mapping as a dispatch table in a hash, where the key would be the user's string and the value is just an anonymous reference to the according sub.

Last edited by Su-Shee; 10-02-2007 at 01:32 PM.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Standard Template Library Reference (std::string) lucky6969b Programming 3 03-15-2006 03:09 PM
Convert a pointer to a reference. AngryLlama Programming 1 07-28-2005 02:58 AM
Problem passing string::size_type by reference (C++) timhardy Programming 2 04-24-2005 05:14 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM


All times are GMT -5. The time now is 12:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration